Searching string in php, search field contains white space

前端 未结 1 564
醉酒成梦
醉酒成梦 2021-01-29 14:59

This is my code

相关标签:
1条回答
  • 2021-01-29 15:43

    To identity an unknown character:

    1. find the string encoding (probably UTF-8 in your case)
    2. extract a short substring with the character
    3. split it into bytes and display them in hexadecimal
    4. looking at the encoding table, find what character it is with the byte(s) used to encode it

    example with the string: "a:\xe2\x80\x85b" that looks like a: b but with a smaller space.

    $str = "a:\xe2\x80\x85b"; // I wrote \xe2\x80\x85 only to set $str and to show a working code,
                              // but here I don't know what are the values of these bytes  
    
    preg_match('~:(.*?)b~us', $str, $m); // shortest substring between : and b
    
    echo implode(' ', array_map(function ($b) { return dechex(ord($b)); }, str_split($m[1])));
    
    // e2 80 85
    

    I obtain the 3 bytes e2 80 85, then I search if it represents one or several characters in the unicode table and I find: U+2005   e2 80 85 FOUR-PER-EM SPACE

    Conclusion: the unknown character is a FOUR-PER-EM SPACE (unicode point: U+2005) and needs the 3 bytes e2 80 85 to be encoded in UTF-8. So I can write it "\xe2\x80\x85" in a double quoted string.

    0 讨论(0)
提交回复
热议问题