Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top