how can i parse a string to remove all non english characters in php
right now I want to remove things like
სოფო ნი�
Thanks :)
how can i parse a string to remove all non english characters in 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('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);
<!--
for example. And those aren't what the OP probably considers "valid EN characters". Upvoted for the creativity.
Oct 2, 2022 at 4:33
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.
use this code:
$illegalChars = array("",);
$string = str_replace($illegalChars,"",$string );
echo $string;