1

我有在 tinymce 中开发的描述。这里有诸如等标签之类的标签<br/><p>我想打印它以显示它的功能。它向我展示的和它一样。

这是输出

<p><strong>Partial Sea and Marina view, Fully Furnished 2 bedroom apartment available for rent in Bahar 1, JBR!<br /></strong><br />

我想要强大的和 p 标记来使强大和段落。这是我的代码

$dess = str_replace("&nbsp;", '',$row['description_demo']);
$dess = str_replace("nbsp;", '',$dess);
echo htmlspecialchars(html_entity_decode(preg_replace("/&#?[a-z0-9]{2,8};/i","",$dess)));
4

1 回答 1

0

使用htmlspecialchars()将禁用所有 HTML 标签。您说您希望<strong>and<p>标记被解释为 HTML,但如果您使用htmlspecialchars()它们,它们必然会被转换为&lt;strong&gt;&lt;p&gt;这将使浏览器实际将文本“ <strong>”和“ <p>”显示为简单文本,而不是解释的 HTML 标记。

您尝试做的似乎更像是允许一些 HTML 标记同时删除其他标记。为此,您不应该使用正则表达式。相反,您需要使用 HTML 解析器,例如HTML Purifier

这是您在示例中使用它的方式:

// Include the HTMLPurifier library
require_once '/path/to/HTMLPurifier.auto.php';

$config = HTMLPurifier_Config::createDefault(); // Set default configuration
$config->set('HTML.Allowed', 'p,strong,br'); // List your allowed HTML tags
$purifier = new HTMLPurifier($config); // Init HTML Purifier with the above setting

$clean_description = $purifier->purify($row['description_demo']); // Purify the HTML from TinyMCE

echo $clean_description; // Check that the output is what you want

干杯

于 2013-09-08T18:13:41.103 回答