如果在单词中有字母“ц”,“щ”,“ы”,“ь”,我必须删除单词。我创建这个函数是为了需要我,但它运作缓慢。
public function CheckToInsert($text)
{
$xarfho = array("ц", "щ", "ы", "ь","қ","ӣ","ғ","ҷ","ҳ","ӯ","Қ","Ӣ","Ғ","Ҷ","Ҳ","Ӯ");
foreach ($xarfho as $xarf)
{
if (stripos($text,$xarf) !== false)
{
return true;
}
}
return false;
}
public function UnsetUncorrectWords($words)
{
foreach ($words as $key => $value)
{
if($this->CheckToInsert($value) == false) unset($words[$key]);
if(strlen($value) < 3) unset($words[$key]);
}
return $words;
}
答案 0 :(得分:2)
我建议重写你的功能(或完全没有使用功能):
public function UnsetUncorrectWords($words)
{
return preg_grep('~\A[^қӣғҷҳӯҚӢҒҶҲӮ]{3,}\z~u', $words);
}
preg_grep
过滤与模式不匹配的数组项。
该模式描述了至少3个字符的单词,不带字母қ,ӣ,ғ,ҷ,ҳ,ӯ,Қ,Ӣ,Ғ,Ҷ,Ҳ,Ӯ。
请注意,您不能将strlen
与多字节字符一起使用,因为这会返回字节数,而不是字符数。
答案 1 :(得分:2)
您可以使用preg_grep
来获取包含正则表达式匹配的数组项,或者不包含与PREG_GREP_INVERT
标记匹配的数组项。
因此,要获得所有没有您选择的字母的项目,请使用
$xarfho = array("ц", "щ", "ы", "ь","қ","ӣ","ғ","ҷ","ҳ","ӯ","Қ","Ӣ","Ғ","Ҷ","Ҳ","Ӯ");
$wrds = array('Еыфвҷ','цӣвееп','аааа');
$pat = '/[' . implode("", $xarfho) . ']/u';
$res = preg_grep($pat, $wrds, PREG_GREP_INVERT);
// => Array ( [2] => аааа )
请参阅PHP demo
要获取带有"ц", "щ", "ы", "ь","қ","ӣ","ғ","ҷ","ҳ","ӯ","Қ","Ӣ","Ғ","Ҷ","Ҳ","Ӯ"
字母的项目,请使用
$xarfho = array("ц", "щ", "ы", "ь","қ","ӣ","ғ","ҷ","ҳ","ӯ","Қ","Ӣ","Ғ","Ҷ","Ҳ","Ӯ");
$wrds = array('Еыфвҷ','цӣвееп','аааа');
$pat = '/[' . implode("", $xarfho) . ']/u';
$res = preg_grep($pat, $wrds);
// => Array ( [0] => Еыфвҷ [1] => цӣвееп )
请参阅another PHP demo。
正则表达式看起来像/[цщы]/u
,其中[...]
是字符类,它匹配模式中定义的任何字符(或字符范围)和{{1}修饰符是必需的,因为您的模式包含ASCII以外的字符,UNICODE修饰符将使正则表达式引擎正确解析模式和输入字符串。