开发者

preg_match unknown modifier '.' in wordpress [duplicate]

This question already has answers here: Warning: preg_replace(): Unknown modifier (3 answers) Closed 3 years ago.

I'm trying to do something simple...I thought?

Have the following...

$current = '/zeta/2010/03/';

preg_match('/zeta/.{4}/(.{2})/$',$current,$monthnum); 

Simply am trying to make $monthnum = 03

Keep either getting an unknown modifier error or, when I add the delimiters '#/zeta/.{4}/(开发者_JAVA技巧.{2})/$# it returns with simply #Array...

Can anyone help?


PCREs have to be enclosed in delimiters.

You have to either escape the inner \ or use another delimiter:

~/zeta/.{4}/(.{2})/$~'

Otherwise, PHP thinks the expression is /zeta/ only and . is not a valid modifier.

preg_match does not return an array. You can inspect the contents of $monthnum with var_dump and access the information you want.

You could have solved your problem by simply reading the documentation:

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

and

preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match.


http://us3.php.net/manual/en/function.preg-match.php

As you can read, the function itself returns true or false if it matches something. When the function returns true, $monthnum will be created as an array, where $monthnum[0] is the complete match, and any following parts you specified to fetch in your regexp will be loaded after that. In your case, the part:

([0-9]{2})

will be loaded into $monthnum[1]

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜