删除非英文字符PHP

Bel*_*ish 21 php character

我如何解析一个字符串来删除PHP中的所有非英语字符

现在我想删除像

სოფოსოფოი

谢谢 :)

aul*_*ron 52

$str = preg_replace('/[^\00-\255]+/u', '', $str);
Run Code Online (Sandbox Code Playgroud)


Ter*_*nen 8

您最好的选择是使用iconv,它将字符串转换为请求的字符编码.

iconv('UTF-8', 'ASCII//TRANSLIT',  $yourtext);
Run Code Online (Sandbox Code Playgroud)

随着//translit你得到一个有意义的转换为ASCII(例如ß - > ss).使用// IGNORE将完全删除非ascii字符.

iconv('UTF-8', 'ASCII//IGNORE',  $yourtext);
Run Code Online (Sandbox Code Playgroud)

http://php.net/manual/en/function.iconv.php


Jim*_* W. 7

通过使用preg_replace()

$string = "some ???? text"; 
$string = preg_replace('/[^a-z0-9_ ]/i', '', $string); 

echo $string;
Run Code Online (Sandbox Code Playgroud)

当然,您需要扩展preg_replace模式,但这是一种方法.可能有更好的方法,我只是不知道.