Friday, September 22, 2006

How do I check whether a string contains HTML tags?

Question :
How do I check whether a string contains HTML tags?

Answer :
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";
}

1 comment:

Pug said...

Thanks for the string comparison technique. Came in handy for me!