regex - Breaking down a image url into three sub parts in PHP -


suppose have image url: http://static2.tripoto.com/media/filter/medium/img/11099/tripdocument/chiang_mai_street_market.jpg

i want extract 3 things url:

  • "medium" (this can medium/small/thumbnail)
  • the part after medium , before image name (img/11099/tripdocument)
  • image name (chiang_mai_street_market.jpg)

also can use regular expression, if want:

preg_match_all('/^.*(medium|small|thumbnail)\/(.*)\/([^\/]+)$/', $url, $matches); 

result:

  • $matches[0] = $url
  • $matches[1] = "medium"
  • $matches[2] = "img/11099/tripdocument"
  • $matches[3] = "chiang_mai_street_market.jpg"

assumption:

  • there none other "medium", "small", or "thumbnail" string in given url, before need.

edit:

to make more flexible fot sizes/phrases, extract phrases array, this:

$sizes = ['medium', 'small', 'thumbnail'];  preg_match_all('/^.*(' . implode('|', $sizes ) . ')\/(.*)\/([^\/]+)$/', $url, $matches); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -