PHP中的substr_replace编码 [英] substr_replace encoding in PHP
本文介绍了PHP中的substr_replace编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?php
$ file =test.txt;
$ writeFile = fopen($ file,w +); //读取/写入
$ myarray = array(δφδφ,δφδσδδσ,δφδφδ);
$ myarray [0] = substr_replace($ myarray [0],ε,0,1);
foreach($ myarray as $ data){
fwrite($ writeFile,$ data。\\\
);
}
?>
OUTCOME
ε φδφ
δφδσφδσ
δφδφδ
OUTCOME WITH NO substr_replace
δφδφ
δφδσφδσ
$
解决方案
b $ b
from shkspr.mobi
function mb_substr_replace($ original,$ replacement,$ position,$ length)
{
$ startString = mb_substr($ original,0,$ position,UTF-8);
$ endString = mb_substr($ original,$ position + $ length,mb_strlen($ original),UTF-8);
$ out = $ startString。 $替换。 $ endString;
return $ out;
}
从 GitHub
函数mb_substr_replace($ str,$ repl,$ start,$ length = null)
{
preg_match_all('/./ us',$ str,$ ar);
preg_match_all('/./ us',$ repl,$ rar);
$ length = is_int($ length)? $ length:utf8_strlen($ str);
array_splice($ ar [0],$ start,$ length,$ rar [0]);
return implode($ ar [0]);
}
我尝试过两者都很好
I want to write to a text file. When I use substr_replace in php the encoding changes. It doesn't print Greek Characters correctly. If I don't everything is fine. Any suggestions?
<?php
$file = "test.txt";
$writeFile = fopen($file, "w+");//read/write
$myarray = array("δφδφ","δφδσφδσ","δφδφδ");
$myarray[0] = substr_replace($myarray[0],"ε", 0,1);
foreach ($myarray as $data){
fwrite($writeFile, $data."\n");
}
?>
OUTCOME
ε�φδφ
δφδσφδσ
δφδφδ
OUTCOME WITH NO substr_replace
δφδφ
δφδσφδσ
δφδφδ
解决方案
You can use these two functions:
from shkspr.mobi
function mb_substr_replace($original, $replacement, $position, $length)
{
$startString = mb_substr($original, 0, $position, "UTF-8");
$endString = mb_substr($original, $position + $length, mb_strlen($original), "UTF-8");
$out = $startString . $replacement . $endString;
return $out;
}
From GitHub
function mb_substr_replace($str, $repl, $start, $length = null)
{
preg_match_all('/./us', $str, $ar);
preg_match_all('/./us', $repl, $rar);
$length = is_int($length) ? $length : utf8_strlen($str);
array_splice($ar[0], $start, $length, $rar[0]);
return implode($ar[0]);
}
I tried both and both work well
这篇关于PHP中的substr_replace编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文