【问题标题】:php - remove html tagsphp - 删除 html 标签
【发布时间】:2013-08-19 10:23:45
【问题描述】:

我在个人项目中使用 contenteditable 功能来更新 sql 数据库,但是当我更新内容时,它会将 html 标签添加到数据库中,即

<div id="lipsum" style="font-size: 11px; font-family: Arial, Helvetica, sans; 
text-align:     justify; font-style: normal; font-variant: normal; line-height: normal;">
 <p style="font-size: 11px; line-height: 14px; margin-bottom: 14px;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin tincidunt tincidunt tellus, 
ac tincidunt magna imperdiet volutpat. Pellentesque pharetra lorem vitae velit gravida, 
eget gravida tellus volutpat. Praesent viverra nulla at arcu fringilla, quis semper ligula 

在去除这些标签方面,我有哪些解决方案?我可以使用 jquery 或 php 吗?谁能给我一些工作示例?

这是我用来更新数据库的代码

保存.php

<?php
include("db.php");
$content = $_POST['content'];
$firstname = $_POST['firstname'];//get posted data
$content = mysql_real_escape_string($content);  
    $firstname = mysql_real_escape_string($firstname);//escape string   

$sql = "UPDATE datadump SET firstname = '$firstname', content = '$content' WHERE id = '1'";
if (mysql_query($sql))
{
    echo 1;
}
?>

js/js.js

 $(document).ready(function() {

    $("#save").click(function (e) {         
        var content = $('#content').html(); 
        var firstname = $('#firstname').html();     
        $.ajax({
            url: 'save.php',
            type: 'POST',
            data: {content: content, firstname: firstname},             
            success:function (data) {

                if (data == '1')
                {
                    $("#status")
                    .addClass("success")
                    .html("Data saved successfully")
                    .fadeIn('fast')
                    .delay(3000)
                    .fadeOut('slow');   
                }

                if (data == '1')
                {
                    $("#status")
                    .addClass("success")
                    .html("Data saved successfully")
                    .fadeIn('fast')
                    .delay(3000)
                    .fadeOut('slow');   
                }
                else
                {
                    $("#status")
                    .addClass("error")
                    .html("An error occured, the data could not be saved")
                    .fadeIn('fast')
                    .delay(3000)
                    .fadeOut('slow');   
                }
            }
        });   

    });

    $("#maincontent").click(function (e) {
        $("#save").show();
        e.stopPropagation();
    });

    $(document).click(function() {
        $("#save").hide();  
    });

});

【问题讨论】:

  • 你在找strip_tags
  • @rob 我想要 strip_tags 而不是 mysql_real_escape_string
  • 你可能想要两者。不过,你真的应该停止使用 mysql_* 函数并开始使用 pdo 或 mysqli。
  • 好的,我明白了,是的,我知道我只是想用简单的 php 快速创建一些东西,看看这是否真的有效,而且它确实是一个非常酷的功能,可以与 @Rob 一起玩跨度>

标签: php jquery contenteditable strip-tags


【解决方案1】:

使用strip_tags() 函数。

改变这个;

$content = mysql_real_escape_string($content);

到这个;

$content = mysql_real_escape_string( strip_tags( $content ) );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多