php - WordPress 中的 preg_match 未知修饰符 '.'

标签 php preg-match

我正在尝试做一些简单的事情...我想?

具有以下...

$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/

相关文章:

regex - 这个正则表达式应该做什么? ^[\d\D]{1,}$

php - 检查字符串是否在php中包含字母

php - 如何使用php从mysql访问图像到html表

php - preg_match 中断超过 5100 个字符

php - preg_match 中的正则表达式和模式 - 适合 123-23-345 的 PHP

php - 按类别解释组织结果

php - 使用 preg_match 查找列表中的所有单词

php - 函数中的可变长度引用参数列表

php - 使用 PHP/MySQL 日期查询向 Google 可视化页面提交表单

php - 自动完成问题