php substr()用于没有空格的utf-8字符

时间:2014-02-12 08:58:30

标签: php substring substr

  <?php
  $utf8string = "سلامجهان";
    echo substr($utf8string,0,5);
?>
    //output سل�

如何修复php中utf-8的substr()?

1 个答案:

答案 0 :(得分:2)

<?php
        $utf8string = "سلامجهان";
        echo mb_substr($utf8string,0,5,'UTF-8');
        //output سلامج
?>

使用mb_substr并为其设置utf-8!

//sample  mb_substr($YourString,first char,length of output char,charset name);