Php preg_match find与多语言的精确匹配不起作用
当我尝试使用缅甸语时,它没有返回正确的数据。我不知道我哪里出了问题。请帮助我找到问题或给我每个事实的解决方案 提前谢谢Php preg_match find与多语言的精确匹配不起作用,php,regex,Php,Regex,当我尝试使用缅甸语时,它没有返回正确的数据。我不知道我哪里出了问题。请帮助我找到问题或给我每个事实的解决方案 提前谢谢 header('Content-Type: text/html; charset=utf-8'); /*$text = " 蝯 榩榿榩榿榩榿 @逯郹酟 勯噚嶢, 潫 橀槶澉 莦莚虙 弣抶 蒆葞 "; //Some text $keyword = "箖緌翢";*/ $text ="အေမရိကန္အေျခစိုက္ Chevrolet ကားကုမၸဏီ၏ ၂၀၁၅ ကို
header('Content-Type: text/html; charset=utf-8');
/*$text = " 蝯 榩榿榩榿榩榿 @逯郹酟 勯噚嶢, 潫 橀槶澉 莦莚虙 弣抶 蒆葞 "; //Some text
$keyword = "箖緌翢";*/
$text ="အေမရိကန္အေျခစိုက္ Chevrolet ကားကုမၸဏီ၏ ၂၀၁၅ ကိုလာ ေမာ္ဒယ္ Chevrolet Malibu";
$keyword = "ကိုလာ";
function find_keywords($text,$search_keywords){
/*
$text : String value
$search_keywords : String value
Function : Seperating search_keywords variable through commna identifier and exact matching.
Return type : boolean
*/
if(empty($search_keywords) || empty($text))
return false;
$search_keywords_arr=explode(",", $search_keywords);
foreach($search_keywords_arr as $keyword) {
//if(preg_match("/\b".trim($keyword)."\b/i", $text) == true) return true;
if(preg_match_all("/(*UTF8)\b($keyword)\b/ui", $text) == true) return true;
}
return false;
}
if(find_keywords($text,$keyword)) echo "Match"; else echo "Not matched";
由于某些多字节字符超出了单词边界字符(
\b
)所涵盖的范围,因此需要手动表示应标识边界的字符
我的代码段将查找字符串的开始/结束、空格字符和unicode标点字符。这应该让您开始,然后您可以根据需要进行修改
代码:()
请把文字粘贴到我们能看到的地方。我认为您可以安全地从模式中删除
(*UTF8)
。右侧的单词边界似乎使模式失败,关键字后面的下一个字符似乎是”ေ"代码>(所以它不是一个空格),整个单词似乎是”ကိုလာ ေ“
(如果我相信单词边界),因为我不懂这种语言,我不能再说更多了,你确定关键字是一个完整的单词吗?确实不需要(*UTF8)
,因为u修饰符已经打开了这个模式。但是,你可以删除u修饰符并添加(*UTF8)(*UCP)
一开始也是一样。我想是在VBA正则表达式中使用了\b
。我不得不使用(\s|^)$关键字(\s|$)
。另外,关于声明(encoding=“utf-8”)
呢?
function find_keywords($text, $search_keywords) {
if (empty($search_keywords) || empty($text)) {
return false;
}
$search_keywords_arr = explode(",", $search_keywords);
foreach ($search_keywords_arr as $keyword) {
if (preg_match('/(?<=^|\s|\p{P})' .preg_quote($keyword, '/') . '(?=\p{P}|\s|$)/ui', $text)) {
return true;
}
}
return false;
}
$text = "အေမရိကန္အေျခစိုက္ Chevrolet ကားကုမၸဏီ၏ ၂၀၁၅ ကိုလာ, ေမာ္ဒယ္ Chevrolet Malibu";
$keyword = "ကိုလာ";
echo find_keywords($text, $keyword) ? "Match" : "Not matched";
Match