我最近发现了strip_tags()
函数,它以字符串和允许的HTML标记列表作为参数。
假设我想要在一个字符串中去掉图片,这里是一个例子:
$html = '<img src="example.png">';
$html = '<p><strong>This should be bold</strong></p>';
$html .= '<p>This is awesome</p>';
$html .= '<strong>This should be bold</strong>';
echo strip_tags($html,"<p>");
返回这个值:
<p>This should be bold</p>
<p>This is awesome</p>
This should be bold
因此, 我已经清除了通过 <strong>
和可能在将来使用的<em>
的格式。
我想要一种黑名单而不是白名单的方式,比如:
echo blacklist_tags($html,"<img>");
返回:
<p><strong>This should be bold<strong></p>
<p>This is awesome</p>
<strong>This should be bold<strong>
有没有任何方法可以做到这一点?
<img>
标签。为了防止意外删除标签,我必须添加每个标签,例如<p><em><strong><blockquote><ul><li>
。 - ThomasReggi