Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_匹配模式超过完全有效值失败_Php_Regex_Wordpress - Fatal编程技术网

Php preg_匹配模式超过完全有效值失败

Php preg_匹配模式超过完全有效值失败,php,regex,wordpress,Php,Regex,Wordpress,我使用regexpal.com对Wordpress试图比较的数据测试了我的regexp,但失败了,看看这个,告诉我你是否发现了问题 regexp "#^json/(.+?)/?([a-zA-Z0-9]*)?$#" 要匹配的内容 json/trips 这些有效,前一个无效 json/trips/0 json/trips/13 json/fullticket/9805048001130122361809 如果我在regexpal中尝试所有这些方法,它们都可以工作,但在wordpress中,只有

我使用regexpal.com对Wordpress试图比较的数据测试了我的regexp,但失败了,看看这个,告诉我你是否发现了问题

regexp

"#^json/(.+?)/?([a-zA-Z0-9]*)?$#"
要匹配的内容

json/trips
这些有效,前一个无效

json/trips/0
json/trips/13
json/fullticket/9805048001130122361809
如果我在regexpal中尝试所有这些方法,它们都可以工作,但在wordpress中,只有不包含我要获取的元素id的方法失败,而其他方法工作正常

有趣的是,$matches返回以下内容:

array
0 => string 'json/trips' (length=10)
1 => string 't' (length=1)
2 => string 'rips' (length=4)

请改用此regexp:

#^json/([^/]+)/?([a-zA-Z0-9]*)?$#
输出:

Array
(
    [0] => json/trips
    [1] => trips
    [2] => 
)

在稍微调整wordpress重写规则之后,答案是:

data/([^/]+)(/([a-zA-Z0-9\-]*))?$

注意:在新的场景中,我将json更改为数据,这样我就不会弄乱自定义的post类型规则

注意,我的一些ID包含字母,我不会删除“a-zA-Z”部分,我实际上应该在其中添加“\-”,因为我的一些ID包含“-”SSORY几秒钟前我找到了我的答案,至少我会投票给你:)