doutuichan2681 2011-10-12 07:08
浏览 42
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • dongqing344716 2011-10-12 07:17
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题