简体   繁体   English

正则表达式从Codeigniter中的URL中排除类别。 怎么样?

[英]Regex to exclude categories from url in Codeigniter. How?

I would like to have 我想拥有

these urls as categories on my page: 这些网址作为我页面上的类别:

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

I am using i18n that is why I have also another links like: 我正在使用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

This is easy, I call a controller articles.php anf inside it I have functions cars, nature, sport and everything works well. 这很容易,我在其中调用了一个controllers.php和an控制器,它具有汽车,自然,运动和一切正常的功能。

However, I want to have articles, no matter what category they are, like this: 但是,无论文章属于什么类别,我都希望有这样的文章:

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

So, the category fell out from url when full article is view. 因此,查看全文时,类别从url中消失了。

I am using i18n so my routes for this look like this: 我正在使用i18n,因此我的路线如下所示:

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

But this call everytime the function show_full_article, because it is in the 2nd segment. 但是每次都调用show_full_article函数,因为它在第二段中。

So, I somehow need to rebuild the regex in: 因此,我需要以某种方式重建正则表达式:

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

to exclude functions cars, nature and sport. 排除汽车,自然和运动功能。 I have only 3 categories so typing it manually is no big deal. 我只有3个类别,因此手动键入没什么大不了的。

Please, if you know how to type the regex in routes.php to exclude these three categories , so codeigniter do not see them anymore as article names, let me know. 请,如果您知道如何在routes.php中键入正则表达式以排除这三个类别,那么codeigniter不再将它们视为文章名称,请告诉我。 I will appreciate it very much. 我会非常感激。

Thank you in advance for any advice. 预先感谢您的任何建议。

You probably want to use a negative lookahead (?!...) for that. 您可能要为此使用负前瞻(?!...)
In your case: 在您的情况下:

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

This ensures that the (.+) can not be one of those words. 这样可以确保(.+)不能是这些单词之一。 It needs to be wrapped in (?:..)$ so it also matches the end of the string, as otherwise the assertion would also block natureSMTHNG and similar longer terms. 它需要包装在(?:..)$以便它也与字符串的末尾匹配,否则断言也将阻止natureSMTHNG和类似的较长术语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM