正则表达式中重音字母在字符串突变中的用法


usage of accented letters in regex in string mutations

如何修改字符串突变的正则表达式代码,使其也适用于重音字母?例如,"amor"正则表达式中的字符串突变应该与"āmōr"的字符串突变相同。我试着简单地包括重音字母,比如´(?<=[aeiouāīṓĕĭŏŭ](´,但这不起作用。

我的代码:

$hyphenation = '~
(?<=[aeiou]) #each syllable contain a vowel
(?:
    # Muta cum liquida
    ( (?:[bcdfgpt]r | [bcfgp] l | ph [lr] | [cpt] h | qu ) [aeiou] x )
  |
    [bcdfghlmnp-tx]
    (?:
        # ct goes together
        [cp] 'K (?=t)
      |
        # two or more consonants are splitted up
        'K (?= [bcdfghlmnp-tx]+ [aeiou]) 
    )   
  |
    # a consonant and a vowel go together
    (?:
        'K (?= [bcdfghlmnp-t] [aeiou])
      | 
        #  "x" goes to the preceding vowel
        x 'K (?= [a-z] | (*SKIP)(*F) ) 
    )
  |
    # two vowels are splitted up except ae oe...
    'K (?= [aeiou] (?<! ae | oe | au | que | qua | quo | qui ) ) 
)
~xi';

// hyphention
$result = preg_replace($hyphenation, '-$1', $input);

带重音的字母可以通过多种方式在unicode中进行配置。例如,ā可以是unicode代码点U+0101(拉丁小写字母A WITH MACRON(,但它也可以是U+0061(拉丁大写字母A(和U+0304(组合MACRON。(链接(

结果,如果:,写入(?<=[aeiouāēīōūăĕĭŏŭ])是正确的

  • 您可以使用u修饰符通知pcreregex引擎,您的字符串和模式必须读取为UTF-8字符串。否则,多字节字符将被视为分离的字节,而不是原子字符(这可能会产生问题,并产生奇怪的结果,尤其是当多字节字符位于字符类中时。例如,[eā]+将匹配"ē"(。

  • 您确信目标字符串和模式对每个字母使用相同的形式。如果模式使用U+0101,字符串U+0061和U+0304表示"ā",则它将不起作用。为了防止出现此问题,可以将$str = Normalizer::normalize($str);应用于主题字符串。这个方法来自intl扩展。

您可以在以下链接中找到更多信息:

https://en.wikipedia.org/wiki/Unicode_equivalence
http://utf8-chartable.de/
http://php.net/manual/en/normalizer.normalize.php
http://php.net/manual/en/reference.pcre.pattern.modifiers.php
http://pcre.org/original/pcre.txt