开发者

Using regex in PHP

I tried this:

$mtcDatum = preg_match("/[0-9]{2}/[0-9]{2}/[0-9]{4}/", $lnURL);

This returns an error message:

开发者_如何学编程

Warning: preg_match() [function.preg-match]: Unknown modifier '['

Why doesn't this work? I'm used to Linux's way of doing regex, does PHP handle regex differently?


PHP syntax is interpreting the "/" character as the end of your pattern. You need to escape the forward slashes:

preg_match("/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/", $lnURL);


You need a delimiter character around your pattern (this is what separates the pattern from any modifiers). Normally / is used, but as this is part of the string you are trying to match, you can use another character such as #:

$mtcDatum = preg_match("#[0-9]{2}/[0-9]{2}/[0-9]{4}#", $lnURL);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜