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 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?