我想写一个文本文件.当我在PHP中使用substr_replace时,编码会发生变化.它不能正确打印希腊字符.如果我不是一切都很好.有什么建议?
<?PHP $file = "test.txt"; $writeFile = fopen($file,"w+");//read/write $myarray = array("δφδφ","δφδσφδσ","δφδφδ"); $myarray[0] = substr_replace($myarray[0],"ε",1); foreach ($myarray as $data){ fwrite($writeFile,$data."\n"); } ?>
结果
εφδφ
δφδσφδσ
δφδφδ
没有substr_replace的结果
δφδφ
δφδσφδσ
δφδφδ
您可以使用以下两个功能:
原文链接:https://www.f2er.com/php/131724.htmlfunction mb_substr_replace($original,$replacement,$position,$length) { $startString = mb_substr($original,"UTF-8"); $endString = mb_substr($original,$position + $length,mb_strlen($original),"UTF-8"); $out = $startString . $replacement . $endString; return $out; }
从GitHub开始
function mb_substr_replace($str,$repl,$start,$length = null) { preg_match_all('/./us',$str,$ar); preg_match_all('/./us',$rar); $length = is_int($length) ? $length : utf8_strlen($str); array_splice($ar[0],$length,$rar[0]); return implode($ar[0]); }
我试过两个都很好