在Codeigniter中从url中排除类别的正则表达式.如何去做


Regex to exclude categories from url in Codeigniter. How?

我想有

这些url在我的页面上作为分类:

http://example.com/en/articles        //for list of all
http://example.com/en/articles/cars    //for list of all cars articles
http://example.com/en/articles/nature  //for list of all nature acrticles
http://example.com/en/articles/sport    //for list of all sport articles

我使用i18n,这就是为什么我也有另一个链接,如:

http://example.com/fr/articles        //for list of all
http://example.com/fr/articles/cars    //for list of all cars articles
http://example.com/fr/articles/nature  //for list of all nature acrticles
http://example.com/fr/articles/sport    //for list of all sport articles

这很简单,我把控制器命名为articles.php,在它里面我有功能汽车,自然,运动和一切都很好。

然而,我想有文章,不管它们是什么类别,像这样:

http://example.com/en/articles/article-about-carshttp://example.com/en/articles/article-about-nature

所以,当完整的文章被查看时,分类从url中掉出来。

我用的是i18n,所以我的路由是这样的:

$route[’^en/articles/(.+)$’] = “articles/show_full_article/$1”;
$route[’^fr/articles/(.+)$’] = “articles/show_full_article/$1”;

但是这个每次调用函数show_full_article,因为它在第二段。

因此,我需要在 中重新构建正则表达式

$路线[' ^ en/文章/(.+)$’] = " 文章/show_full_article/1美元";

排除功能车,自然和运动。我只有3个类别,所以手动输入没什么大不了的。

请,如果你知道如何在routes.php中输入正则表达式来排除这三个类别,所以编码器不再将它们视为文章名称,请告诉我。我将非常感激。

提前感谢您的建议

您可能需要使用负向前看(?!...)
在你的例子中:

 $route['^en/articles/(?!(?:cars|nature|sport)$)(.+)$'] =

这确保(.+)不能是这些单词之一。它需要包装在(?:..)$中,因此它也匹配字符串的末尾,否则断言也将阻止natureSMTHNG和类似的更长的术语。