正则表达式从 Codeigniter 中的 url 中排除类别。如何?

发布于 2024-12-09 12:45:02 字数 1478 浏览 1 评论 0原文

我希望将

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

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 时都会调用此函数,因为它位于第二段。

所以,我需要以某种方式重建正则表达式:

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

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

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

预先感谢您的任何建议。

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:

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.

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

So, the category fell out from url when full article is view.

I am using i18n so my routes for this look like this:

$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.

So, I somehow need to rebuild the regex in:

$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.

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. I will appreciate it very much.

Thank you in advance for any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无畏 2024-12-16 12:45:02

您可能想使用 负向预测 (?!...)< /a>为此。
在您的情况下:

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

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

] =

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

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.

] =

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文