php strip_tags removing everything

筅森魡賤 提交于 2019-12-05 04:50:31

问题


I am using strip tags on user inputs to remove all possible tags but the strip_tags php function is also removing '<' even if not used in a tag.

for example some user might use emoticon as such: >.< or <3

or this can even be used when algorithms etc.

Are there any solution to allow the '<' on strip tags?


回答1:


Problem is in this case

$foo = "text >.<text"

Try this expression (preg_replace with ims falgs):

<\s*\/?[a-z0-9]+(\s*[a-z\-0-9]+)*(\s*[a-z\-0-9]+\="[^"]*")*\s*\/?>

@edit: For example:

<?php 

$test = "text text text >.<asd text <div style=\"font-size:12px;\">text >.<in div</div> asd asd <b>bolded</b> <script> alert('this is javascriptalert'); </script>";

$stripped =  strip_tags($test);
$replaced = preg_replace('/<\s*\/?[a-z0-9]+(\s*[a-z\-0-9]+)*(\s*[a-z\-0-9]+\="[^"]*")*\s*\/?>/ims','',$test);
var_dump($stripped,$replaced);



回答2:


You can use regular expression:

$result = preg_replace("/\<\/?[A-Za-z\-\_]+\>/", "", YOUR_DATA);



回答3:


The HTML parser uses < to denote the start of an element, so generally you're safer not using it anyway. If you want a less than sign to appear in your copy, use the HTML name or number instead: &lt; or &#60;.




回答4:


No, strip_tags will work that way. There is no difference between an opening brace and a smiley part, so you can add exceptions before strip_tags:

input.replace("<3", "& lt;3");
input = strip_tags(input);


来源:https://stackoverflow.com/questions/33297154/php-strip-tags-removing-everything

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!