php strip_tags:允许 <br />?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3793978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php strip_tags: allows <br />?
提问by laukok
How it is possible to allow <br />
in strip_tags() or any way I can get around to it?
如何允许<br />
strip_tags() 或我可以解决的任何方式?
<?php
$text = '<p>Test <br />paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";
// Allow <p>, <a>, <br />
echo strip_tags($text, '<p><a><br />');
echo "\n";
// Allow <br /> only
echo strip_tags($text, '<br />');
?>
result:
结果:
Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>
Test paragraph. Other text
Thanks, Lau
谢谢,刘
回答by Randy the Dev
Don't use a self-closing tag name? echo strip_tags($text, '<br>');
不使用自闭合标签名称? echo strip_tags($text, '<br>');
The strip_tags()
function's allowable_tags
argument takes the allowed tags in the form <tagname>
The reason your code didn't work was because you used <br />
instead of <br>
.
该strip_tags()
函数的allowable_tags
参数采用表单中允许的标签<tagname>
The 原因您的代码不起作用是因为您使用<br />
了<br>
.
回答by bobince
strip_tags
is not intended as a security measure, and using it with allowable_tags
is definitely insecure, as it'll let through event handler and other harmful attributes.
strip_tags
不是作为一种安全措施,使用它allowable_tags
绝对是不安全的,因为它会让事件处理程序和其他有害属性通过。
If you want to allow user input with a few whitelisted elements and attributes you'll need to use a HTML-sanitising library with a proper HTML parser. See for example HTML purifier.
如果您想允许用户输入一些列入白名单的元素和属性,您需要使用带有适当 HTML 解析器的 HTML 清理库。参见示例HTML 净化器。
It's usually better for user comments not to give the user control over the HTML markup at all, but instead to accept raw text, HTML-escape it on output, and do replacements to generate markup from text (eg: \n
-> <br>
, \n\n
-> </p><p>
, link detection).
用户评论通常最好不要让用户完全控制 HTML 标记,而是接受原始文本,在输出时对它进行 HTML 转义,并进行替换以从文本生成标记(例如:\n
-> <br>
、\n\n
-> </p><p>
、链接检测)。
回答by Ronald
Whitespace is also not allowed in tags: http://php.net/manual/en/function.strip-tags.php(see 2nd note)
标签中也不允许使用空格:http: //php.net/manual/en/function.strip-tags.php(见第二条注释)
回答by Pramendra Gupta
Ya you can mix one or more tag to be striped same time.
是的,您可以同时混合一个或多个标签以进行条纹化。
string strip_tags ( string $str [, string $allowable_tags ] )
check documentation
检查文件
if you want to strip new line as well solution will be before stripping you can use nl2br
如果您还想剥离新行,解决方案将是剥离之前,您可以使用nl2br
so
所以
echo strip_tags(nl2br($text), '<br>');