Thursday, November 24, 2011

Check whether text contain html tags using php

There are two ways you can do it, one is using a regular expression and the other is comparing strings.

First the regular expression approach:

if (preg_match("/([\<])([^\>]{1,})*([\>])/i", $string)) {
echo "string contains html";
}

Second a comparing string approach:

if(strlen($string) != strlen(strip_tags($string)){
echo "string contains HTML";
}

No comments:

Post a Comment