Remove unmatched HTML tags in a string

People know that someone knows the PHP function to remove unsurpassed HTML tags from a string. e.g. <div> This is a string <b> with an unmatched bold tag </div> . If not, then help me increase it, maybe I will have a function that counts the number of opening tags and the corresponding closing tags. If they donโ€™t even delete the first opening tag or close the tags anymore, does it delete the last tag?

+2
source share
2 answers

I do not believe that there is a function. What you want to do is use something like tidy that supports PHP (PHP Tidy). Tidy will clear your HTML for you. Also, donโ€™t think about it using regular expressions !;)

Here is a Zend tutorial that talks about how to tidy up your HTML code:

+3
source

Without adhering to any rule structure, this is not very possible. If you want to follow the standards (i.e. Don't exit </b> from the blocking block), you can do a regular expression scan to make sure that </b> found before </div> .

http://www.regular-expressions.info/lookaround.html

0
source

Source: https://habr.com/ru/post/1236741/


All Articles