Check If A String Is HTML In PHP


Read {count} times since 2020

HTML detection can be hard, because HTML is complicated and there aren’t any good 100% parsers for HTML. RegEx won’t help you in detection, so you need something else. The trick to identify HTML which I’m gonna show you is very easy. It’s fast, short and doesn’t use RegEx. You can simply use an if statement to check if a string is HTML or not.

PHP’s strip_tags function when given a string, it returns a string that doesn’t have any HTML / PHP characters (according to the manual). So, if we give a HTML string to strip_tags(), it strips out the HTML code from it and return a normal string.

So, if the original value is the same as the return value of strip_tags(), then that string doesn’t have any HTML code. Here is how you check if a string is HTML :

function isHTML($string){
 if($string != strip_tags($string)){
  // is HTML
  return true;
 }else{
  // not HTML
  return false;
 }
}

Or if you need a simple short function, use this code :

function isHTML($string){
 return $string != strip_tags($string) ? true:false;
}

Here are some usages of this function :

$html = "<b>subinsb.com is a programming blog</b>";
if(isHTML($html)){
 echo "It's HTML";
}

The above code will surely print It’s HTML since the string we gave was HTML.

$notHTML = "subinsb.com is online for 3 years";
if(!is_HTML($notHTML)){
 echo "Not HTML";
}

Note the false checking character “!” in the above code. Of course, it will print Not HTML since the string we gave didn’t had any HTML code in it.

Show Comments