[英]How to find category-name, page number in http://domain.com/post-type/category/category-name/page/2
I have an URL formatted like so: 我有一个格式如下的URL:
http://domain.com/post-type/category/category-name/page/2
And need to extract only category-name
no matter what follows. 并且无论如何都需要提取
category-name
。
For example, in the url: 例如,在网址中:
http://domain.com/projects/category/print/page/2
I only want the word 我只想要这个词
print
I am trying this regex: category\\/(.+)\\/(?(?=(?:category)\\/)^|(.+))\\/(.+)\\/?$
cobbled together via some searching, but I really am quite confused by Regex. 我正在尝试这个正则表达式:
category\\/(.+)\\/(?(?=(?:category)\\/)^|(.+))\\/(.+)\\/?$
通过一些搜索拼凑在一起,但我对Regex感到非常困惑。
http://regex101.com/r/uG8pV8 http://regex101.com/r/uG8pV8
Note 注意
I am executing this in PHP (in what I think is regex, correct?). 我在PHP中执行此操作(我认为是正则表达式,对吗?)。 At the moment, the Wordpress code is as so:
$newrules['projects/category/(.*/?)$'] = 'index.php?post_type=project&project_cat=$matches[1]';
目前,Wordpress代码如下:
$newrules['projects/category/(.*/?)$'] = 'index.php?post_type=project&project_cat=$matches[1]';
which seems to take the entire URL after projects/category/
在
projects/category/
之后似乎占用整个URL
Edit 编辑
Is it also possible to get the page number as well? 是否也可以获取页码?
eg: http://domain.com/post-type/category/category-name/page/2
例如:
http://domain.com/post-type/category/category-name/page/2
: http://domain.com/post-type/category/category-name/page/2
Is it possible to get only category-name
and 2
as separate matches? 是否可以只将
category-name
和2
作为单独的匹配?
You've to use an ungreedy matcher .+?
你要使用一个不合适的匹配器
.+?
the ?
的
?
behind .+
means ungready and it stops matching after finding the first patterm behind itself. 背后
.+
意味着已经完成并且在找到第一个背后的问题后停止匹配。 In this case this is /.+
. 在这种情况下,这是
/.+
。
/category\/(.+?)\/.+/
你可以试试这个正则表达式。
(?<=http:\/\/domain\.com\/projects\/category\/)([^\/]+)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.