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

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.

图片转代码服务由CSDN问答提供 功能建议

我有一个字符串,其中包含或多或少的2000个字符并包含URL(URL网络)。 我希望从这个URL获取具有正则表达式的特定值,但我不知道如何使用它们。

字符串的一部分将是这样的:

  $ string = [...] http:// localhost / project / gallery / http:// localhost / project / case / category#/ img#/ 情况#/ HTTP://本地主机/项目/壳体/类别#/ IMG#/壳体#/ HTTP://本地主机/项目/站点地图[...] 
上  
 
  

我想要的是为所有出现 的每个不同类别,图像和/或案例获取与类别#/ img#/ case#/ 匹配的所有内容(例如: “main-category / 45 / main /”和“secondary-category / 51 / id12”)

如果类别#超过1个单词,它加上“ - ”(例如:“main-category”)

img#总是一个2到3位的数字(例如:“45”)。

案例#与纯文本不同(已加入 用“ - ”如果超过1个字)到4到5个字符长的字符串,用两个第一个c haracters为“id”,其他2到3个字符为数字(例如1:“main-case”,eg2:“id45”)

有一点 需要注意的是,如果这对任何人有帮助,那么我想要得到的是案例/ 总是

I 希望答案/回复不包含带有子句等的循环。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题