php substr() function with utf-8 leaves � marks at the end

前端 未结 7 1413
野的像风
野的像风 2020-11-27 05:32

Here is simple code



        
7条回答
  •  攒了一身酷
    2020-11-27 05:40

    Never use constant in substr function for UTF-8 string:

    $st = substr($text, $beg, 100);
    

    50% chance you will get half of a character at end of the string.

    Do it like this:

    $postion_degin = strpos($text, $first_symbol);
    $postion_end = strpos($text, $last_symbol);
    $len = $postion_end - $postion_degin + 1;
    $st = substr($text, $postion_degin, $len);
    

    100% safe result.

    No mb_substr.

提交回复
热议问题