strpos stops working properly after using htmlspecialchars on the "haystack"

  htmlspecialchars, php, strpos

I’ve run into a problem I am searching for a substring in a string with strpos and everything works fine, but after I use htmlspecialchars on my string it stops working, and I have no clue why.

This is my minimal code reproduction:

    $input = "Hello&.Hello";
    $input = htmlspecialchars($input, ENT_QUOTES, 'UTF-8');

    if (strpos($input, "&.") !== false) {
        echo "Word found";
    } else {
        echo "No Word found";
    }

When I use it without htmlspecialchars the word is found but when I use it like in the example nothing is found

Aditational Information:
When i only search for the & or only for the . it works, but both combined don’t.
I also tried looking for the replacement of the & combined with the . like so: &amp. but this didn’t work either.

I hope someone can help me, Thanks

Source: Ask PHP

LEAVE A COMMENT