是否有一个正则表达式来匹配一个包含或不包含特殊字符的特定字符串?
Like sql database keywords=ال ح مد will match after search highlight=ال ح م د
编辑:我想匹配带有和不带有特殊/重音字符的特定字符串。不仅仅是任何字符串/字符。
function mbStringToArray($string)
{
$strlen = mb_strlen($string);
while($strlen)
{
$array[] = mb_substr($string, 0, 1, "UTF-8");
$string = mb_substr($string, 1, $strlen, "UTF-8");
$strlen = mb_strlen($string);
}
return $array;
}
// I had to use this ugly function to remove accents as iconv didn't work properly on my test server.
function stripAccents($stripAccents){
return utf8_encode(strtr(utf8_decode($stripAccents),utf8_decode('ِ', 'ُ', 'ٓ', 'ٰ', 'ْ', 'ٌ', 'ٍ', 'ً', 'ّ', 'َ'),''));
}
$keywords= 'الحمد';
$search = '%' . $keywords . '%';
$stmt = $mysqli->prepare("SELECT * FROM quran WHERE $trans LIKE ?");
$stmt->bind_param('s', $search);
$stmt->execute();
$stmt->store_result();
$clientNameNoAccent = stripAccents($clientName);
$clientNameArray = mbStringToArray($clientName);
foreach($clientNameArray as $pos => &$char)
{
$charNA =$clientNameNoAccent[$pos];
if($char != $charNA)
{
$char = "(?:$char|$charNA|$charNA\p{M})";
}
}
$clientSearchPattern = implode($clientNameArray); // c(?:é|e|e\p{M})ra
if($stmt->execute()){
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){
$text = 'الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ';
$search = preg_replace('/(.*?)(' . $clientSearchPattern . ')(.*?)/iu', '$1<span class="highlight">$2</span>$3', $text);
echo $search;// لِلَّهِ رَبِّ الْعَالَمِينَ<span class="highlight">الْحَمْدُ</span>
}
}
1条答案
按热度按时间4xrmg8kj1#
我创建了一个方法来为关键字(由用户输入)生成动态正则表达式模式,以考虑阿拉伯语或波斯语(波斯语)口音(diacritics/tashkeel marks/eraab)。这可以用于突出显示字符串中的关键字和/或基于关键字在搜索结果中显示的长文本。我希望这对某些人有帮助!