简体   繁体   English

删除非英文字符PHP

[英]Remove Non English Characters PHP

how can i parse a string to remove all non english characters in php 我如何解析一个字符串来删除PHP中的所有非英语字符

right now I want to remove things like 现在我想删除像

სოფო ნი სოფოსოფოი

Thanks :) 谢谢 :)

$str = preg_replace('/[^\00-\255]+/u', '', $str);

Your best option would be using iconv , which converts strings to requested character encoding. 您最好的选择是使用iconv ,它将字符串转换为请求的字符编码。

iconv('UTF-8', 'ASCII//TRANSLIT',  $yourtext);

with //translit you get a meaningful conversion to ASCII (eg ß -> ss). 使用//translit您可以获得有意义的ASCII转换(例如ß - > ss)。 Using //IGNORE will strip non-ascii characters altogether. 使用// IGNORE将完全删除非ascii字符。

iconv('UTF-8', 'ASCII//IGNORE',  $yourtext);

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

By using preg_replace() 通过使用preg_replace()

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

echo $string;

Granted, you will need to expand the preg_replace pattern, but that is one way to do it. 当然,您需要扩展preg_replace模式,但这是一种方法。 There is probably a better way, I just do not know it. 可能有更好的方法,我只是不知道。

use this code: 使用此代码:

$illegalChars = array("",); 
$string  = str_replace($illegalChars,"",$string );
echo $string;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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