如何从 PHP 中的字符串中删除 HTML 转义序列字符?

How to remove HTML escape sequence characters from the string in PHP?

假设,我有一个包含此类字符串的以下字符串变量。

$sample_String = "Dummy User graded your comment \"\r\n\t\t\t\t\tdoc_ck.docx\r\n\t\t\t\t\tDownload\r\n\t\t\t\t\t\" that you posted.";

现在我不希望字符串中出现这些 HTML 个字符。

我应该如何以高效可靠的方式删除它们?我想要最终的输出字符串如下:

$sample_String = "Dummy User graded your comment \"doc_ck.docx Download\" that you posted.";

当它在浏览器中显示时,出现在 " 之前的 '\' 将消失,浏览器中的字符串将如下所示:

虚拟用户对您发布的评论 "doc_ck.docx Download" 进行了评分。

不是吗?

谢谢。

到目前为止我已经尝试了下面的代码但没有成功:

function br2nl($buff = '') {
    $buff = mb_convert_encoding($buff, 'HTML-ENTITIES', "UTF-8");
    $buff = preg_replace('#<br[/\s]*>#si', "\n", $buff);
    $buff = trim($buff);

    return $buff;
  }
$sample_String = br2nl(stripslashes(strip_tags($sample_String)));

如果你只想删除\r(carrige return)\n(换行符)和\t(制表符)你可以这样做:

$string = "Dummy User graded your comment \"\r\n\t\t\t\t\tdoc_ck.docx\r\n\t\t\t\t\tDownload\r\n\t\t\t\t\t\" that you posted.";
$string = str_replace(array("\r", "\n", "\t"), "", $string);

如果您想保留换行符(并让它们显示在浏览器中),请执行以下操作:

$string = "Dummy User graded your comment \"\r\n\t\t\t\t\tdoc_ck.docx\r\n\t\t\t\t\tDownload\r\n\t\t\t\t\t\" that you posted.";
$string = nl2br(str_replace(array("\r", "\t"), "", $string));

HTMLentities 是像 &quot;&#063;

这样的序列

像这样使用正则表达式:

<?php
$str = "Dummy User graded your comment \"\r\n\t\t\t\t\tdoc_ck.docx\r\n\t\t\t\t\tDownload\r\n\t\t\t\t\t\" that you posted.";
echo preg_replace('/[\r\n\t]+/m','',$str);