Remove Non English Characters PHP

给你一囗甜甜゛ 提交于 2019-12-17 10:37:16

问题


how can i parse a string to remove all non english characters in php

right now I want to remove things like

სოფო ნი�

Thanks :)


回答1:


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



回答2:


Your best option would be using iconv, which converts strings to requested character encoding.

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

with //translit you get a meaningful conversion to ASCII (e.g. ß -> ss). Using //IGNORE will strip non-ascii characters altogether.

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

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




回答3:


By using 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. There is probably a better way, I just do not know it.




回答4:


use this code:

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


来源:https://stackoverflow.com/questions/3654879/remove-non-english-characters-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!