我有一个 <textarea>
我允许提交用户内容的地方。我想允许几个标签,例如 <b>
, <i>
, <blockquote>
, 和 <del>
.但是,由于内容将显示在页面中,因此我必须确保没有未关闭的标签。
我知道我可以使用 strip_tags($textarea, '<b><i><blockquote><del>')
,但我怎样才能确保所有剩余的标签都正确关闭?
最佳答案
你可以使用 Tidy .它将清理和清理您的 HTML。
php.net 上的这条评论解决了您的问题并展示了解决方法:http://www.php.net/manual/en/tidy.examples.basic.php#89334
Cleaning an html fragment (OO support seems to half-arsed for now)
This will ensure all tags are closed, without adding any html/head/body tags around it.
<?php
$tidy_config = array(
'clean' => true,
'output-xhtml' => true,
'show-body-only' => true,
'wrap' => 0,
);
$tidy = tidy_parse_string($html_fragment, $tidy_config, 'UTF8');
$tidy->cleanRepair();
echo $tidy;
?>
关于php - 确保所有标签都关闭 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5433262/