我有一个 preg_match_all 函数来获取标签:
preg_match_all('/(^|[^a-z0-9_])#([a-z0-9_]+)/ui', $text, $matchedHashtags);
这个程序效果良好,但是如果遇到一些重音符号就会停止运行:
#hash //works
#hash_hash //works
#hash_não // get just $hash_n
我需要改变什么?
您可以使用Unicode类别Letter
。
\p{L}
匹配来自所有Unicode脚本(≅ 语言)的字母。
正则表达式:
/(?<![\p{L}0-9_])#([\p{L}0-9_]+)/u
Another option is to simply include the accented characters in the character class:
/(?<![a-zãàÀèÈìÌ0-9_])#([a-zãàÀèÈìÌ0-9_]+)/ui
[编辑]
\p
令牌php在设置/u
标志时支持Unicode。有一些替代方案来匹配字母,使用以下类别或脚本:
\p{Latin}
拉丁文脚本\p{L&}
无大小写字母\p{L}
所有字母\p{Xan}
任何字母数字字符\p{L}
和 \p{N}
数字(来自任意脚本)。\p{Xwd}
任何Perl“单词”字符\p{Xan}
和下划线。
\p{Latin}
来缩小字符集到拉丁字母,或者只返回翻译后的文本。 - Casimir et Hippolyte\p{Mn}
。我不认为这是在这种情况下适用的。 - Mariano