0

我正在尝试创建一个博客系统,我想将博客文章数据保存到 mysql 但很少混淆如何清理或清理数据这里是我尝试过的

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  return $data;
}

现在,当我通过 ajax 发布博客数据时,它看起来像

<h1 class="text-center">
    Write the titles of article here
</h1>

然后我清理帖子数据并回显它

echo $title = mysqli_real_escape_string($connecDB, test_input($_POST['page__title']));

然后这是我得到的回应

\r\n    Write the titles of article here\r\n

我想知道为什么我在响应中得到这些 \r\n 我怎样才能摆脱它们,这是清理 html 帖子的最佳方式

注意:我想将数据保存为 mysql 中的 html

我已经有一个正则表达式,可以找到样式和脚本标签并删除它,我也尝试删除条形斜杠并尝试添加 strip_tags 但我仍然得到 \r\n

4

1 回答 1

0

你得到\r\n是因为mysql_real_escape_string()用相应的转义序列替换控制字符。此函数仅用于将变量替换为 MySQL 查询时使用,它不应用于以 HTML 格式输出。清理 HTML 输出的正确函数是htmlentities().

$title = test_input($_POST['page__title']);
echo htmlentities($title);

如果要在输出中保留换行符,还应该使用nl2br().

echo nl2br(htmlentities($title));
于 2016-06-23T02:03:06.293 回答