phpregexreplacepathurl-parsing

Insert new directory into a URL string just before the filename


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?


Solution

  • preg_replace('![^/]+$!', 's720x720/$0', $str);