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:
database:
my question character come from?
- 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.
you need tell
mb_
functions data in utf-8 can treat correctly. either set globally functions usingmb_internal_encoding
, or pass$encoding
parameter function when call it:mb_substr($_tmpstr, 0, $i, 'utf-8')
Comments
Post a Comment