PHP 打印中的 HTML 标签问题


html tags issue in php print

我有在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)));

使用 htmlspecialchars() 将禁用所有 HTML 标记。你说你希望<strong><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

干杯

相关文章: