问题
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}/(.{2})/$#
it returns with simply #Array
...
Can anyone help?
回答1:
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.
回答2:
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]
来源:https://stackoverflow.com/questions/6206001/preg-match-unknown-modifier-in-wordpress