我的正则表达式无法识别阿拉伯字符
My regex doesn't recognize arabic character
我想停止重复一个字符超过 3 次,将其转换为 juste 3 次。
示例:aaaaaaaaaaall => aaall
所以,我用英文字符做到了,而且成功了:
$patternReplace = '/(.){3,}/i';
$chaine = preg_replace($patternReplace, "", $chaine, -1 );
但它不适用于阿拉伯字符,例如:
أأأأأأأأأأأأأأأأأأأأأأأأأأأأ
感谢帮助
使用 u
修饰符:
$patternReplace = '/(.){3,}/iu';
$chaine = preg_replace($patternReplace, "", $chaine, -1 );
来自 PHP.net 的文档:
u (PCRE_UTF8)
This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern and subject strings are treated as UTF-8. This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern and the subject is checked since PHP 4.3.5. An invalid subject will cause the preg_* function to match nothing; an invalid pattern will trigger an error of level E_WARNING. Five and six octet UTF-8 sequences are regarded as invalid since PHP 5.3.4 (resp. PCRE 7.3 2007-08-28); formerly those have been regarded as valid UTF-8.
我想停止重复一个字符超过 3 次,将其转换为 juste 3 次。
示例:aaaaaaaaaaall => aaall
所以,我用英文字符做到了,而且成功了:
$patternReplace = '/(.){3,}/i';
$chaine = preg_replace($patternReplace, "", $chaine, -1 );
但它不适用于阿拉伯字符,例如:
أأأأأأأأأأأأأأأأأأأأأأأأأأأأ
感谢帮助
使用 u
修饰符:
$patternReplace = '/(.){3,}/iu';
$chaine = preg_replace($patternReplace, "", $chaine, -1 );
来自 PHP.net 的文档:
u (PCRE_UTF8)
This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern and subject strings are treated as UTF-8. This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern and the subject is checked since PHP 4.3.5. An invalid subject will cause the preg_* function to match nothing; an invalid pattern will trigger an error of level E_WARNING. Five and six octet UTF-8 sequences are regarded as invalid since PHP 5.3.4 (resp. PCRE 7.3 2007-08-28); formerly those have been regarded as valid UTF-8.