【问题标题】:Regex to exclude categories from url in Codeigniter. How?正则表达式从 Codeigniter 中的 url 中排除类别。如何?
【发布时间】:2011-10-12 07:08:32
【问题描述】:

我想要

这些网址作为我页面上的类别:

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 anf 我有汽车、自然、运动等功能,一切都很好。

但是,我想要文章,不管它们是什么类别,像这样:

http://example.com/en/articles/article-about-cars http://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 时都会调用它,因为它在第 2 段中。

所以,我需要在以下位置重建正则表达式:

$route['^en/articles/(.+)$'] = "articles/show_full_article/$1";

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

如果您知道如何在 routes.php 中键入正则表达式以排除这三个类别,那么 codeigniter 不再将它们视为文章名称,请告诉我。我会非常感激的。

提前感谢您的任何建议。

【问题讨论】:

标签: php codeigniter


【解决方案1】:

您可能想为此使用negative lookahead (?!...)
在你的情况下:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多