我想要将句子中的每个土耳其字母替换为英语字母,我尝试以下函数:
$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;我认为土耳其字符的问题,但我不知道我如何才能让php正确地读取这些字符来正确地进行替换。我需要你的建议??
发布于 2013-10-25 14:45:11
您是否注意到打印的是$turkish而不是被替换的字符串(即$final_title)?您可以看到一个数组,因为您正在打印该数组。如果您在浏览器上单独打印数组,您可能会看到那些乱七八糟的字符,因为输出不是UTF-8编码的。但是,如果您这样做(请注意meta标记):
<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);您将看到正确的字符。但这不是问题所在。str_replace()运行良好。它应该工作得很好。
发布于 2017-10-25 01:27:40
那大字母呢,我的解决方案是:
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要生成i-ll-make-all-happen而不是list make-all-happen,只需将‘\’和'"‘添加到$before的列表中,并将’‘和’‘添加到after的列表中
https://stackoverflow.com/questions/19582497
复制相似问题