0

In the code below I am trying to replace turkish special characters like "ğ,ş,ç" to "g,s,c". When I try to convert an internal string it is ok. but when I try to convert a posted string nothing changes. Here is the code:

    <?php

$meslek0=$_POST['meslek']; 

$internal='ş';
echo '<br>internal original: '.$internal;

echo '<br>posted original: '.$meslek0;

echo '<br>posted after decode: '.$meslek0;

$meslek0=replace_tr($meslek0);

$internal=replace_tr($internal);

echo '<br>internal converted:'.$internal;
echo '<br>posted converted:'.$meslek0;




function replace_tr($text) {
$text = trim($text);
$search = array('Ç','ç','Ğ','ğ','ı','İ','Ö','ö','Ş','ş','Ü','ü',' ');
$replace = array('c','c','g','g','i','i','o','o','s','s','u','u','-');
$new_text = str_replace($search,$replace,$text);
return $new_text;
}  



 ?>

1 Answers1

0

I think these chars are in UTF-8. The standard String-Functions in PHP are not made for characters with more than 1 byte.

On PHP.net, someone proposed this function:

function mb_str_replace($needle, $replacement, $haystack) {
   return implode($replacement, mb_split($needle, $haystack));
}

Using this instead of str_replace() should work

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
  • Thanks for the answer. I tried, it returned some mistakes. But what is funny is the string $internal is converted succesfully. when I enter same letters in a textbox and post to $meslek0 it is not converted. – mekmelov Mar 20 '15 at 06:13
  • I guess, $_POST['meslek'] contains data of a
    ? Does it have the attribute accept-charset="UTF-8"?
    – FelixSFD Mar 20 '15 at 09:38