0

str_replace 不会用没有重音的字母替换重音字母。那有什么问题?

这将返回预期结果:

<?php
    $string = get_post_custom_values ("text");
    // Say get_post_custom_values ​​("text") equals "José José"
    $string = str_replace(" ", "-", $string);

    echo $string [0];
    // Output "José-José"
?>

这不起作用:

<?php
    $string = get_post_custom_values ("text");
    // Say get_post_custom_values ​​("text") equals "Joseph Joseph"
    $string = str_replace("é", "e", $string);

    echo $string [0];
    // Output "José José". Nothing has changed
?>

注意:使用谷歌翻译从葡萄牙语翻译而来。

4

2 回答 2

1

删除每个重音字母的简单、安全的方法是使用 iconv :

setlocale(LC_ALL, "fr_CA.utf8"); // for instance
$output = iconv("utf-8", "ascii//TRANSLIT", $input);

您当前的问题很可能是由不同的编码引起的。

于 2012-03-04T04:07:07.030 回答
0

é保存在源代码中的字符与您从中获取的数据的编码get_post_custom_values不同。编码不匹配 → 不被识别为相同字符 → 未被替换。

于 2012-03-04T03:57:36.297 回答