Php mb_ereg_match 错误匹配
Php mb_ereg_match faulty match
我正在尝试将某些文本与 php 的 mb_ereg_match 匹配,并且我正在使用这段正则表达式来匹配所有非 Word 聊天:
/[^-\w.]|[_]/u
我希望能够查找 unicode 字符,这就是我使用 mb_ereg 的原因。
使用此输入:
'γιωρ;γος.gr'
其中包含来自希腊字母表的字符。
我想匹配';'如果匹配到 return -1 else return 输入。
无论我做什么,它都不匹配';'和 returns 输入。
我尝试使用 preg_match 但它在我工作时不起作用。
有什么建议吗?
编辑 1
我做了一个测试,我发现如果我将输入转换为:
,它会完全匹配
';γος.gr'
也适用于拉丁字符。
编辑 2
如果我得到以下之一,我想打印 -1.
'γιωρ;γος.gr'
';γος.gr'
'γιωρ;.gr'
';.gr'
否则我想得到任何输入。
编辑 3
我做了一些更多的测试,它不匹配任何被 utf-8 字符包围的特殊字符。
您需要使用 \X
和 preg_match_all
来匹配所有 Unicode 字符:
\X
- an extended Unicode sequence
此外,请参阅此 \X
description from Regular-Expression.info:
Matching a single grapheme, whether it's encoded as a single code point, or as multiple code points using combining marks, is easy in Perl, PCRE, PHP, and Ruby 2.0: simply use \X
. You can consider \X
the Unicode version of the dot. There is one difference, though: \X
always matches line break characters, whereas the dot does not match line break characters unless you enable the dot matches newline matching mode.
然后您可以使用以下代码段:
$re = '/\X/u';
$str = "γιωρ;γος.gr";
preg_match_all($re, $str, $matches);
if (in_array(";", $matches[0])) {
echo -1;
}
else {
print_r($matches[0]);
}
我正在尝试将某些文本与 php 的 mb_ereg_match 匹配,并且我正在使用这段正则表达式来匹配所有非 Word 聊天:
/[^-\w.]|[_]/u
我希望能够查找 unicode 字符,这就是我使用 mb_ereg 的原因。 使用此输入:
'γιωρ;γος.gr'
其中包含来自希腊字母表的字符。
我想匹配';'如果匹配到 return -1 else return 输入。
无论我做什么,它都不匹配';'和 returns 输入。
我尝试使用 preg_match 但它在我工作时不起作用。
有什么建议吗?
编辑 1
我做了一个测试,我发现如果我将输入转换为:
';γος.gr'
也适用于拉丁字符。
编辑 2
如果我得到以下之一,我想打印 -1.
'γιωρ;γος.gr'
';γος.gr'
'γιωρ;.gr'
';.gr'
否则我想得到任何输入。
编辑 3
我做了一些更多的测试,它不匹配任何被 utf-8 字符包围的特殊字符。
您需要使用 \X
和 preg_match_all
来匹配所有 Unicode 字符:
\X
- an extended Unicode sequence
此外,请参阅此 \X
description from Regular-Expression.info:
Matching a single grapheme, whether it's encoded as a single code point, or as multiple code points using combining marks, is easy in Perl, PCRE, PHP, and Ruby 2.0: simply use
\X
. You can consider\X
the Unicode version of the dot. There is one difference, though:\X
always matches line break characters, whereas the dot does not match line break characters unless you enable the dot matches newline matching mode.
然后您可以使用以下代码段:
$re = '/\X/u';
$str = "γιωρ;γος.gr";
preg_match_all($re, $str, $matches);
if (in_array(";", $matches[0])) {
echo -1;
}
else {
print_r($matches[0]);
}