regexp与俄罗斯郎

vor*_*bey 8 php regex unicode cyrillic

我用regexp无法解决我的问题.

好的,当我输入:

$string = preg_replace("#\[name=([a-zA-Z0-9 .-]+)*]#","$name_start $1 $name_end",$string);
Run Code Online (Sandbox Code Playgroud)

一切都很好,除了俄语的情况.

所以,我尝试重新输入这个reg-exp:

$string = preg_replace("#\[name=([a-zA-Z0-9**?-??-?** .-]+)*]#","$name_start $1 $name_end",$string);
Run Code Online (Sandbox Code Playgroud)

但这不起作用,

我知道一些想法,只写:

$string = preg_replace("#\[name=([a-zA-Z0-9???????????????????????????????? .-]+)*]#","$name_start $1 $name_end",$string);
Run Code Online (Sandbox Code Playgroud)

但这很疯狂:D

拜托,给我简单的变体

Bar*_*ers 18

尝试使用Unicode范围:

'/[\x{0410}-\x{042F}]/u'  // matches a capital cyrillic letter in the range A to Ya
Run Code Online (Sandbox Code Playgroud)

不要忘记Unicode的/ u标志.

在你的情况下:

"#\[name=([a-zA-Z0-9\x{0430}-\x{044F}\x{0410}-\x{042F} .-]+)*]#u"
Run Code Online (Sandbox Code Playgroud)

请注意,正则表达式中的STAR是多余的.一切都已经被PLUS"吃掉"了.这会做同样的事情:

"#\[name=([a-zA-Z0-9\x{0430}-\x{044F}\x{0410}-\x{042F} .-]+)]#u"
Run Code Online (Sandbox Code Playgroud)


Vol*_*erK 6

常见的unicode脚本(自pcre 3.3起支持)为Cyrillic属性提供测试.

例如,替换既不是西里尔字母也不是拉丁字母的所有字符:

$string = '1a2b3c?d?e?f??x?y';
echo preg_replace('/[^0-9\p{Cyrillic}]/u', '*', $string);
Run Code Online (Sandbox Code Playgroud)

您可以在http://www.pcre.org/pcre.txt"Unicode字符属性" 下找到该功能的文档.
您必须按照http://docs.php.net/reference.pcre.pattern.modifiers中的说明指定PCRE8标志(u).