I'm creating a BBcode type of function which takes out all html code from a form input and then converts [b][/b] to actual bold tags, [u] to actual u tags, and [i] to actual i tags.
What concerns me, however, is if whoever writes and submits the input doesn't close all the tags. I don't want that to mess up the entire page when the info is displayed later.
How would you recommend I automatically close all the tags (only b, i, and u are allowed) with the function? Is there a way to count how many [b] and how many [/b] there are and if there's a difference add that many [/b] to the end? Or is there an easier way?
BTW, I haven't tried anything yet because the only thing I can think of is to count how many [b] there are, count how many [/b] there are, get the difference between the two and make a loop that many times adding the closing tag. But I don't know how to do the first part of that (returning how many [b] there are).
If someone is willing to enlighten me on how to do that (I'm a noob I know) I will get right on trying it and let you know how it goes. :)
There are different possibilities.
You can scan the content with different libraries such as "HTMLTidy" which would remove any unclosed tags
Also you could count all tags and if they are not closed and simply append a close tag for each unclosed tag to the content dynamically. preg_match could help you here...
Another idea would be to isolate the part with the user-written content into an iframe, that would cause that broken HTML wouldnt affect any elements outside the page.