Search code examples
phphtml-entitiesstrpos

strpos returning false with html entities even when using html_entity_decode


I'm trying to search for a substring in a string that may or may not contain html entities such as   but even when using html_entity_decode() it still returns false

$str='online shop';
echo html_entity_decode($str);
//outputs: online shop
var_dump(strpos(html_entity_decode($str),'online shop'));
//outputs: bool(false)

Solution

  •   !== space (20), it actully equals 2 byte NO-BREAK SPACE when decoded C2 A0.

    U+00A0 | \xc2\xa0 |   | NO-BREAK SPACE

    You can test this with:

    <?php
    //
    $str='&nbsp;';
    var_dump(html_entity_decode($str));
    

    Which will yield: string(2) " "

    To fix replace the nbsp space with a normal one:

    var_dump(strpos(str_replace("\xc2\xa0", ' ', html_entity_decode($str)), 'online shop'));

    Result: int(0)

    https://3v4l.org/WXTGR