douturan1807 2013-12-10 17:54
浏览 132
已采纳

PHP正则表达式在URL中的斜杠之间获取特定值

I have a string which has, more or less, 2000 characters and contains URLs (a railway of urls). I want to get specific values from this URL with regex but I have no idea how to use them.

A part of the string would be like this:

$string = [...]http://localhost/project/gallery/http://localhost/project/case/category#/img#/case#/http://localhost/project/case/category#/img#/case#/http://localhost/project/sitemap[...]

What I want is to get everything that matches category#/img#/case#/ for every different category, image and/or case of all occurrences (eg: "main-category/45/main/" and "secondary-category/51/id12").

If category# is more than 1 word, it's joined with a "-" (eg: "main-category").

img# is always a 2 to 3 digit number (eg: "45").

case# varies from plain text (joined with "-" if more than 1 word) to a 4 to 5 characters long string with the two first characters being "id" and the other 2 to 3 characters as numbers (eg1: "main-case", eg2: "id45").

One thing to note, if that helps anyone, is that what I want to get is always after case/.

I would like answers/replies that do not contain loops with substr etc.

  • 写回答

1条回答 默认 最新

  • dongxiaoshe0737 2013-12-10 17:57
    关注

    You can use this regex to match your input URI:

    '~/case/([^/]+)/(\d{2,3})/([^/]+)/~i'
    

    With matching group #1 as category, group #2 as img# and group #3 as case3

    Working demo: http://ideone.com/xrsVtf

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

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)