【发布时间】:2015-01-16 20:22:19
【问题描述】:
我正在尝试编写 wordpress 漂亮的永久链接正则表达式。
我有以下网址。我需要两根火柴,
1st : 在 get/ 之前 / 和 / 之间的最后一个单词 第二个:以 get/ 开头的字符串 网址可能是这样的
http://localhost/akasia/yacht-technical-services/yacht-crew/get/gulets/for/sale/
这里我需要“yacht-crew”和“get/gulets/for/sale/”
http://localhost/akasia/testimonials/get/motoryachts/for/sale/
这里我需要“推荐”和获取/motoryachts/for/sale/
http://localhost/akasia/may/be/lots/of/seperator/but/ineed/last/get/ships/for/rent/
这里我需要“last”和 get/ships/for/rent/
$url1 = 'somepage/get/gulets/for/sale'; // I need somepage and get/gulets/for/sale
$url2 = 'somepage/subpage/get/motoryachts/for/rent'; // I need subpage and get/motoryachts/for/rent
$url3 = 'may/be/unlimited/directories/but/i/need/here/get/ships/for/sale'; // I need here and get/ships/for/sale
$url4 = 'services/get/gulets/for/sale'; // I need services and get/gulets/for/sale
$regex = '([^\/]+?)\/(get\/.+)';
// I can not change anything below.
preg_match("#^$regex#", $url4, $matches);
echo "<pre>";
print_r($matches);
echo "</pre>"
我捕捉到了 $url4 但无法捕捉到 $url1、$url2、$url3
如果有人提供帮助,我将不胜感激。
【问题讨论】:
-
@AvinashRaj 非常感谢您的帮助。但是,如果您仔细查看 preg_march(^$regex) 处的 ^ ,我无法更改。如果你使用 like ^([^\/]+?)\/(get\/.+) 它只匹配 $url1。不是其他人
-
是的,不要使用
^锚点,因为^断言我们处于起步阶段。我想你自己解决了这个问题。我没有注意到您的preg_match通话中出现了^锚。 -
@AvinashRaj 我不这样使用它。 Wordpress 就是这样工作的。我无法更改核心文件。我只能使用钩子
标签: regex wordpress mod-rewrite permalinks