我正在尝试做一些简单的事情...我想?
具有以下...
$current = '/zeta/2010/03/';
preg_match('/zeta/.{4}/(.{2})/$',$current,$monthnum);
我只是想制作 $monthnum = 03
继续收到未知修饰符错误,或者当我添加分隔符时 '#/zeta/.{4}/(.{2})/$#
它简单地返回 #Array
...
有人可以帮忙吗?
最佳答案
PCRE 必须包含在分隔符中。
您必须转义内部 \
或使用其他分隔符:
~/zeta/.{4}/(.{2})/$~'
否则,PHP 认为表达式仅为 /zeta/
,而 .
不是 valid modifier .
preg_match
不返回数组。您可以使用 var_dump
检查 $monthnum
的内容并访问您想要的信息。
您只需阅读文档就可以解决您的问题:
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.
和
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.
关于php - WordPress 中的 preg_match 未知修饰符 '.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6206001/