2012-06-28 12 views
5

Chcę napisać do pliku tekstowego. Kiedy używam substr_replace w php, zmienia się kodowanie. Nie wyświetla poprawnie greckich znaków. Jeśli nie, wszystko jest w porządku. Jakieś sugestie?kodowanie substr_replace w PHP

<?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"); 
} 
?> 

WYNIKI
εφδφ
δφδσφδσ
δφδφδ

WYNIKI BEZ substr_replace
δφδφ
δφδσφδσ
δφδφδ

+1

Można spróbować tej funkcji wielobajtowy http://lv.php.net/manual/en/function.substr-replace.php#59544 – arma

+0

Tak, sam kodowanie –

+1

Musisz użyć funkcji wielobajtowych, aby zrobić to, co chcesz w tym przypadku. Prosty 'substr_replace' działa tylko na danych jako ciąg binarny - bez dbania o kodowanie. – Christian

Odpowiedz

0

Można spróbować użyć funkcji mb_convert_encoding() do s et prawidłowe kodowanie.

3

Zakładając, że kodujesz grekę w kodowaniu wielobajtowym (takim jak UTF-8), to nie zadziała, ponieważ podstawowe funkcje PHP, w tym substr_replace, nie są wielobajtowe. Traktują jedną postać jako równą jednemu bajtowi, co oznacza, że ​​w końcu podzielimy wielobajtowe znaki na połowę, jeśli wymienimy tylko ich pierwszy bajt. Trzeba użyć bardziej ręcznego podejścia obejmującego wielo-bajtowy ciąg funkcji świadomy jak mb_substr:

mb_internal_encoding('UTF-8'); 
echo 'ε' . mb_substr('δφδφ', 1); 

The comment @arma links to w komentarzach owija tę funkcjonalność w funkcji.

3

Spróbuj tej wersji:

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; 
} 
+0

ta funkcja jest buggy – evilReiko

+0

Chcesz wyjaśnić @evilReiko? –

+0

Próbowałem, działało dobrze, ale potem zauważyłem, że czasami usuwa pierwszą postać po wymianie – evilReiko

0
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; 
} 
4

Można używać tych dwóch funkcji:

z 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; 
} 

Od 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]); 
} 

próbowałem obu i oba działają dobrze