Search code examples
phpregexstringexpressionwhitelist

remove Russian letters from a string in php


How can i remove all Russian letters from a string in PHP ?
Or the opposite, i would like to keep only.
English letters, white space, numbers and all the signs like !@#$%^&*(){}":?><>~'"

How can i accomplish that, Thank you.

i figure it out, i replace all Russian cherecters with ### and then i substring from the start to the end.

$desc = preg_replace('/[а-я]+/iu','###', $desc);

$start = strpos ($desc,'###');
$end =strrpos ($desc,"###");

if($start!==false)
{
    $descStart = substr($desc,0,$start);
    $descEnd = substr($desc,$end+3);
    $desc = $descStart.$descEnd;
}

Solution

  • $string = 'тест тест Тест Обязателльно Stackoverflow >!<';
    var_dump(preg_replace('/[\x{0410}-\x{042F}]+.*[\x{0410}-\x{042F}]+/iu', '', $string));
    

    Input string must be in unicode, and output be in unicode too