正则表达式忽略重音?.PHP


Regex to ignore accents? PHP

有没有办法制作一个忽略重音的正则表达式?

例如:

preg_replace("/$word/i", "<b>$word</b>", $str);

正则表达式中的"i"是忽略区分大小写的,但无论如何都可以匹配,例如
爪哇雅瓦

我确实尝试复制$str,将内容更改为无重音字符串并找到所有出现的索引。但是 2 个字符串的索引似乎不同,即使它只是没有重音。

(我做了一个研究,但我能找到的只是如何从字符串中删除重音)

我不

认为有这样的方法。这将取决于区域设置,您可能希望首先使用"/u"开关以在模式字符串中启用 UTF-8。

我可能会做这样的事情。

function prepare($pattern)
{
   $replacements = Array("a" => "[áàäâ]",
                         "e" => "[éèëê]" ...);
   return str_replace(array_keys($replacements), $replacements, $pattern);  
}
pcre_replace("/(" . prepare($word) . ")/ui", "<b>''1</b>", $str);

在您的情况下,索引是不同的,因为除非您使用mb_string否则您可能正在处理每个字符使用多个字节的 UTF-8。

Java 和 Jávã 是不同的词,正则表达式中没有用于删除重音的原生支持,但您可以在正则表达式中包含要替换的带或不带重音符的所有可能字符组合。

喜欢preg_replace("/java|Jávã|jáva|javã/i", "<b>$word</b>", $str);.

祝你好运!

Regex 不是适合您的工具。

您正在寻找的答案是strtr()功能。

此函数替换字符串中的指定字符,这正是您要查找的字符。

在您的示例中,Jávã ,您可以使用如下所示的strtr()调用:

$replacements = array('á'=>'a', 'ã'=>'a');
$output = strtr("Jávã",$replacements);

$output现在将包含Java .

当然,你需要一个更大的$replacements数组来处理所有你想要使用的角色。请参阅我链接的手册页,了解人们如何使用它的一些示例。

请注意,没有一个简单的一揽子字符列表,因为首先它会很大,其次,相同的起始字符可能需要在不同的上下文或语言中进行不同的翻译。

希望有帮助。

<?php
if (!function_exists('htmlspecialchars_decode')) {
    function htmlspecialchars_decode($text) {
        return str_replace(array('&lt;','&gt;','&quot;','&amp;'),array('<','>','"','&'),$text);
    }
}
function removeMarkings($text) 
{
    $text=htmlentities($text);    
    // components (key+value = entity name, replace with key)
    $table1=array(
        'a'=>'grave|acute|circ|tilde|uml|ring',
        'ae'=>'lig',
        'c'=>'cedil',
        'e'=>'grave|acute|circ|uml',
        'i'=>'grave|acute|circ|uml',
        'n'=>'tilde',
        'o'=>'grave|acute|circ|tilde|uml|slash',
        's'=>'zlig', // maybe szlig=>ss would be more accurate?
        'u'=>'grave|acute|circ|uml',
        'y'=>'acute'
    );
    // direct (key = entity, replace with value)
    $table2=array(
        '&ETH;'=>'D',   // not sure about these character replacements
        '&eth;'=>'d',   // is an ð pronounced like a 'd'?
        '&THORN;'=>'B', // is a þ pronounced like a 'b'?
        '&thorn;'=>'b'  // don't think so, but the symbols looked like a d,b so...
    );
    foreach ($table1 as $k=>$v) $text=preg_replace("/&($k)($v);/i",''1',$text);
    $text=str_replace(array_keys($table2),$table2,$text);    
    return htmlspecialchars_decode($text);
}
$text="Here two words, one in normal way and another in accent mode java and jává and me searched with java and it found both occurences(higlighted form this sentence) java and jává<br/>";
$find="java"; //The word going to higlight,trying to higlight both java and jává by this seacrh word
$text=utf8_decode($text);
$find=removeMarkings(utf8_decode($find)); $len=strlen($find);
preg_match_all('/'b'.preg_quote($find).''b/i', removeMarkings($text), $matches, PREG_OFFSET_CAPTURE);
$start=0; $newtext="";
foreach ($matches[0] as $m) {
    $pos=$m[1];
    $newtext.=substr($text,$start,$pos-$start);
    $newtext.="<b>".substr($text,$pos,$len)."</b>";
    $start=$pos+$len;
}
$newtext.=substr($text,$start);
echo "<blockquote>",$newtext,"</blockquote>";
?>

我认为这样的事情会对你有所帮助,我从一个论坛上得到了这个......看看。

设置适当的区域设置(例如 fr_FR),并使用 strcoll 函数比较忽略重音的字符串。