Search code examples
phpunicodestrpos

strpos() returns false when needle string contains an invisible / zero-width character


Why it is not returning good?

<?php
$r = "<h3>Welcome</h3>";
if (strpos($r, "​Welcome") !== FALSE) {
    echo "Good";
}
?>

Reproduce by hardcoding the offending unicode character which was destroyed when creating this post:

$r = "<h3>Welcome</h3>";
if (strpos($r, "\u{200C}Welcome") !== FALSE) {
    echo "Good";
}

Solution

  • Do you see the little box? That is E2 80 8B in UTF-8, the zero-width space.
    And that's not present in your source text.


    enter image description here