[英]PHP's filter_var() Function Generating Warning
Does any body know why the filter_var() function below is generating the warning? 有谁知道为什么下面的filter_var()函数会生成警告? Is there a limit on how many characters can be in a character class?
字符类中可以包含多少个字符?
$regex = "/^[\w\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057\072\073\074\075\076\077\100\133\134\135\136\140\173\174\175\176]*$/";
$string = "abc";
if(!filter_var($string, FILTER_VALIDATE_REGEXP, array("options" => array("regexp"=>$regex))))
{
echo "dirty";
}
else
{
echo "clean";
}
Warning: filter_var() [function.filter-var]: Unknown modifier ':' 警告:filter_var()[function.filter-var]:未知修饰符':'
Your regex is interpreted by PHP as this string : PHP将您的正则表达式解释为以下字符串:
string '/^[\w!"#$%&'()*+,-./:;<=>?@[\]^`{|}~]*$/' (length=40)
(use var_dump
on $regex, and you'll get that) (在$ regex上使用
var_dump
,您会得到的)
Right in the middle of your regex, so, there is a slash ; 就在正则表达式中间,正斜线; as you are using a slash to delimit the regex (it's the first character of
$regex
) , PHP thinks this slash in the middle is marking the end of the regex. 当您使用斜杠来分隔正则表达式时(这是
$regex
的第一个字符) ,PHP认为中间的斜杠标志着正则表达式的结束。
So, PHP thinks your regex is actually : 因此,PHP认为您的正则表达式实际上是:
/^[\w!"#$%&'()*+,-./
Every character that comes after the ending slash are interpreted as modifiers . 斜杠后的每个字符都被解释为修饰符 。
And ':' is not a valid modifier. 而且“:”不是有效的修饰符。
You might want to escape the slash in the middle of the regex ;-) 您可能想在正则表达式中间转义斜线;-)
As well as some other characters, btw... 以及其他一些字符,顺便说一句...
A solution for that might be to use the preg_quote
function. 一个解决方案可能是使用
preg_quote
函数。
这是当前的正则表达式:
/^[\w\041\042\043\044\045\046\047\050\051\052\053\054\134\055\056\134\057\072\073\074\075\076\077\100\133\134\134\134\135\134\136\140\173\174\175\176]*$/i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.