0

I have an image URL that is in the form of:

http://sub-domain.example.net/random-folder-here/4422414324235_4234234.jpg 

and I would like to add a folder just before the image's filename (s720x720) to achieve this result:

http://sub-domain.example.net/random-folder-here/s720x720/4422414324235_4234234.jpg 

How do I run a regex call in PHP that will help me do this?

1
  • You don't need regex, substr with strrpos would be easier. Commented Dec 29, 2012 at 20:54

2 Answers 2

1
preg_replace('![^/]+$!', 's720x720/$0', $str); 
Sign up to request clarification or add additional context in comments.

1 Comment

[^/] means anything but /, + means 1 or more, $ means end of line, and the ! are the delimiters.
1

You want to add s720x720 between the dirname and the basename (if taken the URL as a path). PHP has a function to out the URI apart here (see pathinfo):

$url = 'http://sub-domain.example.net/random-folder-here/4422414324235_4234234.jpg'; vprintf("%s/s720x720/%s", pathinfo($url)); 

Output:

http://sub-domain.example.net/random-folder-here/s720x720/4422414324235_4234234.jpg 

I hope this helps. There is not always a need to use regular expressions. Other functions can be more fitting.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.