问题
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
δφδφ
δφδσφδσ
δφδφδ
回答1:
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
回答2:
Assuming you're encoding the Greek in a multi-byte encoding (like UTF-8), this won't work because the core PHP string functions, including substr_replace
, are not multi-byte aware. They treat one character as equal to one byte, which means you'll end up slicing multi-byte characters in half if you only replace their first byte. You need to use a more manual approach involving a multi-byte aware string function like mb_substr:
mb_internal_encoding('UTF-8');
echo 'ε' . mb_substr('δφδφ', 1);
The comment @arma links to in the comments wraps that functionality in a function.
回答3:
Try this version:
function mb_substr_replace ($string, $replacement, $start, $length = 0)
{
if (is_array($string))
{
foreach ($string as $i => $val)
{
$repl = is_array ($replacement) ? $replacement[$i] : $replacement;
$st = is_array ($start) ? $start[$i] : $start;
$len = is_array ($length) ? $length[$i] : $length;
$string[$i] = mb_substr_replace ($val, $repl, $st, $len);
}
return $string;
}
$result = mb_substr ($string, 0, $start, 'UTF-8');
$result .= $replacement;
if ($length > 0) {
$result .= mb_substr ($string, ($start+$length+1), mb_strlen($string, 'UTF-8'), 'UTF-8');
}
return $result;
}
回答4:
function replace($string, $replacement, $start, $length = 0)
{
$result = mb_substr ($string, 0, $start, 'UTF-8');
$result .= $replacement;
if ($length > 0)
{
$result .= mb_substr($string, ($start + $length), null, 'UTF-8');
}
return $result;
}
回答5:
You could try using the mb_convert_encoding()
function to set the correct encoding.
来源:https://stackoverflow.com/questions/11239597/substr-replace-encoding-in-php