更新时间:2023-11-26 21:01:10
也许你需要检查字符串的格式是否正确.
我会使用这样的函数
函数检查($string) {$start =strpos($string, '<');$end =strrpos($string, '>',$start);$len=strlen($string);如果 ($end !== false) {$string = substr($string, $start);} 别的 {$string = substr($string, $start, $len-$start);}libxml_use_internal_errors(true);libxml_clear_errors();$xml = simplexml_load_string($string);返回计数(libxml_get_errors())==0;}
只是一个警告:html 允许像下面这样的不平衡字符串.它不是一个 xml 有效块,但它是一个合法的 html 块
- Hi
- ;我是另一个 li</li></ul>
免责声明我已经修改了代码(未经测试).为了检测字符串中格式正确的html.
最后一次也许你应该使用 strip_tags 来控制用户输入(就像我一样在您的评论中看到)
Which function in php validate if the string is html? My target to take input from user and check if input html and not just string.
Example for not html string:
sdkjshdk<div>jd</h3>ivdfadfsdf or sdkjshdkivdfadfsdf
Example for html string:
<div>sdfsdfsdf<label>dghdhdgh</label> fdsgfgdfgfd</div>
Thanks
Maybe you need to check if the string is well formed.
I would use a function like this
function check($string) {
$start =strpos($string, '<');
$end =strrpos($string, '>',$start);
$len=strlen($string);
if ($end !== false) {
$string = substr($string, $start);
} else {
$string = substr($string, $start, $len-$start);
}
libxml_use_internal_errors(true);
libxml_clear_errors();
$xml = simplexml_load_string($string);
return count(libxml_get_errors())==0;
}
Just a warning: html permits unbalanced string like the following one. It is not an xml valid chunk but it is a legal html chunk
<ul><li>Hi<li> I'm another li</li></ul>
Disclaimer I've modified the code (without testing it). in order to detect well formed html inside the string.
A last though Maybe you should use strip_tags to control user input (As I've seen in your comments)