简体   繁体   中英

Replace Turkish letters with English letters

I want to replace every Turkish letter in a sentence with an english letter , I try the following function :

$title_result = "Türkiye'nin en iyi oranlari ile Lider Bahis Sitesi";
$turkish = array("ı", "ğ", "ü", "ş", "ö", "ç");//turkish letters
$english   = array("i", "g", "u", "s", "o", "c");//english cooridinators letters

$final_title = str_replace($turkish, $english, $title_result);//replace php function
print_r($turkish);//when printing this i got: Array ( [0] => ı [1] => ğ [2] => ü [3] => ş [4] => ö [5] => ç ) 
return $final_title;

i think the problem in the Turkish characters but i do not know how i can make the php read this characters correctly to make the replacement properly . I need you suggestions please??

Have you noticed that you're printing $turkish and not the replaced string (ie $final_title )? You see an array because you're printing the array. If you're printing the array alone on your browser, you see those messed-up characters probably because the output isn't UTF-8 encoded. However if you do this (notice the meta tag):

<meta charset="utf-8" />
<?php
$title_result = "Türkiye'nin en iyi oranlari ile Lider Bahis Sitesi";
$turkish = array("ı", "ğ", "ü", "ş", "ö", "ç");//turkish letters
$english   = array("i", "g", "u", "s", "o", "c");//english cooridinators letters

$final_title = str_replace($turkish, $english, $title_result);//replace php function
print_r($turkish);

You will see the characters correctly. But that's not the matter. The str_replace() works fine. It should work fine.

What about grand letter, My solution is:

function url_make($str){
    $before = array('ı', 'ğ', 'ü', 'ş', 'ö', 'ç', 'İ', 'Ğ', 'Ü', 'Ö', 'Ç'); // , '\'', '""'
    $after   = array('i', 'g', 'u', 's', 'o', 'c', 'i', 'g', 'u', 'o', 'c'); // , '', ''

    $clean = str_replace($before, $after, $str);
    $clean = preg_replace('/[^a-zA-Z0-9 ]/', '', $clean);
    $clean = preg_replace('!\s+!', '-', $clean);
    $clean = strtolower(trim($clean, '-'));

return $clean;
}

echo url_make('Bu Çocuğu Kim İşe Aldı'); // bu-cocugu-kim-ise-aldi
echo url_make('Birisi"nin adı'); // birisinin-adi
echo url_make("I'll make all happen"); // ill-make-all-happen

To produce i-ll-make-all-happen instead of ill-make-all-happen, just add '\\'' and '"' to the list of $before, and add ' ' and ' ' to the list of after

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM