简体   繁体   中英

php substr() for utf-8 characters without whitespace

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

How to fix substr() for utf-8 in php?

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

Use mb_substr and set utf-8 for it!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM