zabbarob zabbarob - 3 years ago 83
HTML Question

How to identify if a text is HTML or not? (in PHP)

I want to read text entries from a database, some of which are actually HTML entries, others are just plain text that might contain HTML markup which should be displayed as text.

Those that are plain text should then be converted to HTML, by first calling PHP's

function and then running the result through HTMLPurifier.

Or in other words, I'm looking for some tips on how to implement the isHTML() function:

$text = getTextFromDatabase();
if (!isHTML($text)) {
$text = htmlspecialchars($text);
$purifier = new HTMLPurifier();
$clean_html = $purifier->purify($text);

So for example following text would be run through

The <p> tag of HTML has to be followed by a </p> tag to end the paragraph.

And following text would not be run through

<p>These are few lines of HTML.</p>
<div>There might be multiple independent</div>
<p>but valid HTML blocks in it.</p>

It seems like there should already be an
function out there, but I just can't happen to find it and I don't want to reinvent the wheel :-). Maybe it's even possible to do this with some kind of HTMLPurifier settings?

Note that, if the HTML code is buggy, this should be handled by HTMLPurifier and the code should not be run through
. :-) Like for example having an opening
tag when there really should be a closing
tag in the HTML code.

Any help is appreciated, thanks already :-),


Answer Source

you can only check for chars specific for html in string

function is_html($string)
  return preg_match("/<[^<]+>/",$string,$m) != 0;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download