Which function in php validate if the string is valid html?

前端 未结 7 1902
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 16:30

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:

7条回答
  •  时光说笑
    2020-12-03 16:54

    Do you mean HTML or XHTML?

    The HTML standard and interpretation are so loose that your first snippet might work. It won't be pretty but you might get something.

    XHTML is quite a bit more strict and at minimum will expect your snippet to be well-formed (all opened tags are closed; tags can nest but not overlap) and may throw warnings if you have unrecognized elements or attributes.

    Something like Tidy - http://php.net/manual/en/book.tidy.php - is probably a good start. Once you load your snippet using that, you can use tidy_error_count or tidy_get_error_buffer to see if it's "okay enough" for your needs.

提交回复
热议问题