substr_replace encoding in PHP -
i want write text file. when use substr_replace in php encoding changes. doesn't print greek characters correctly. if don't fine. suggestions?
<?php $file = "test.txt"; $writefile = fopen($file, "w+");//read/write $myarray = array("δφδφ","δφδσφδσ","δφδφδ"); $myarray[0] = substr_replace($myarray[0],"ε", 0,1); foreach ($myarray $data){ fwrite($writefile, $data."\n"); } ?>
outcome
ε�φδφ
δφδσφδσ
δφδφδ
outcome no substr_replace
δφδφ
δφδσφδσ
δφδφδ
assuming you're encoding greek in multi-byte encoding (like utf-8), won't work because core php string functions, including substr_replace
, not multi-byte aware. treat 1 character equal 1 byte, means you'll end slicing multi-byte characters in half if replace first byte. need use more manual approach involving multi-byte aware string function mb_substr
:
mb_internal_encoding('utf-8'); echo 'ε' . mb_substr('δφδφ', 1);
the comment @arma links to in comments wraps functionality in function.
Comments
Post a Comment