用PHP替换é和e

Jas*_*rst 3 php

嘿,这是我在stackoverflow上的第一篇文章.

我试图用e和其他类似的特殊字符替换é.我已经尝试过str_replace()并将其从UTF-8转换为ASCII,但似乎没有任何效果.当我将它从UTF-8转换为任何东西时,它只会将é关掉.当我使用str_replace()时,它永远不会捕获它并且é仍然存在.

我感觉我们的服务器内部出现了问题,因为我的朋友在他的服务器上尝试了str_replace()并且工作正常.

谢谢,

杰森托尔赫斯特

Wri*_*ken 7

$string = iconv('utf-8','ASCII//IGNORE//TRANSLIT',$string);
Run Code Online (Sandbox Code Playgroud)


TRi*_*RiG 6

您可以使用htmlentities()to 转换éé,然后使用正则表达式在 & 号后拉出第一个字符。

function RemoveAccents($string) {
    // From http://theserverpages.com/php/manual/en/function.str-replace.php
    $string = htmlentities($string);
    return preg_replace("/&([a-z])[a-z]+;/i", "$1", $string);
}
Run Code Online (Sandbox Code Playgroud)