Search code examples
phphtmlstringstrpos

PHP - If string contains '<' or '>' - strpos()


I want to do a simple kind of test to see if a string contains any HTML.

In this case if the $string variable is test or <test> it returns no for me:

$string = 'test';
if(strpos($string,'<') !== 'false'){
    echo 'no';
}else{
    echo 'yes';
}

Is there a better way to check if a string contains HTML? I don't want to do anything to the string just check if it has HTML tags?


Solution

  • if($string != strip_tags($string)) {
        // contains HTML
    }
    

    Took the answer from here