-3

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?

9
  • 3
    !== false - compare to boolean not a string Commented Aug 13, 2018 at 11:06
  • I think you should use an HTML Parser. '<' is not mandatory a HTML tag. Commented Aug 13, 2018 at 11:08
  • definitely a typo question Commented Aug 13, 2018 at 11:09
  • @Jens I think you'll find the < is pretty much mandatory when opening any html tag Commented Aug 13, 2018 at 11:10
  • @Dale yes, but the presence of < doesn't guarantee the string contains HTML. Commented Aug 13, 2018 at 11:10

1 Answer 1

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

Took the answer from here

Sign up to request clarification or add additional context in comments.

1 Comment

Please mark as duplicate, instead of duplicating answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.