Title: Basic php Regex Help for changing url structure
Tags: php;regex
Question: I have an image that is of the form:
http://sub-domain.example.net/random-folder-here/4422414324235_4234234.jpg
and I would like to add a folder in the middle of the url like this (s720x720 added)
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?
Thanks!
Comment: You don't need regex, [substr](http://us3.php.net/manual/en/function.substr.php) with [strrpos](http://php.net/manual/en/function.strrpos.php) would be easier.

Here is the accepted answer: preg_replace('![^/]+$!', 's720x720/$0', $str);
Comment for this answer: `[^/]` means anything but `/`, `+` means 1 or more, `$` means end of line, and the `!` are the delimiters.

Here is another answer: 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.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:12/29/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><regex>', 'Title': 'Basic php Regex Help for changing url structure', 'ViewCount': 97, 'id': 14085793, 'user_AboutMe': '', 'user_AccountId': 2060931.0, 'user_DisplayName': 'Risha', 'user_Reputation': 147.0, 'user_Views': 9.0, 'user_id': 1837478.0}}