php - Corrupted data using UTF-8 and mb_substr -


i'm data mysql db, varchar(255) utf8_general_ci field , try write text pdf php. need determine string length in pdf limit output of text in table. noticed output of mb_substr/substr strange.

for example:

mb_internal_encoding("utf-8");  $_tmpstr = $vfrow['title']; $_tmpstrlen = mb_strlen($vfrow['title']); for($i=$_tmpstrlen; $i >= 0; $i--){      file_put_contents('cutoffattributes.txt',$vfrow['field']." ".$_tmpstr."\n",file_append);      file_put_contents('cutoffattributes.txt',$vfrow['field']." ".mb_substr($_tmpstr, 0, $i)."\n",file_append); } 

outputs this:

screen shot npp

npp file link

database:

enter image description here enter image description here

my question character come from?

  1. you need ensure you're getting data database in utf-8 encoding setting connection encoding appropriately. depends on database adapter, see utf-8 way through details.
  2. you need tell mb_ functions data in utf-8 can treat correctly. either set globally functions using mb_internal_encoding, or pass $encoding parameter function when call it:

    mb_substr($_tmpstr, 0, $i, 'utf-8') 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -