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:
Thanks for the string comparison technique. Came in handy for me!
Post a Comment