I need to replace a root relative URL with a different root relative URL:
/Images/filename.jpg
should be replaced with:
/new/images-dir/filename.jpg
I started by using PHP's str_replace
function:
$newText = str_replace('/Images/', '/new/images-dir/', $text);
...but then I realized that it was replacing my absolute URLs that I don't want replaced:
http://sub.domain.com/something/Images/filename.jpg
#...is being replaced with...
http://sub.domain.com/something/new/images-dir/filename.jpg
So then I switched to using PHP's preg_replace
function so I can use a regular expression to selectively replace only the root relative URLs and not the absolute URLs. However, I can't seem to figure out the syntax to do this:
$text = 'There is a root relative URL here: <img src="/Images/filename.jpg">'
. 'and an absolute here: <img src="http://sub.domain.com/something/Images/filename.jpg">'
. 'and one not in quotes: /Images/filename.jpg';
$newText = preg_replace('#/Images/#', '/new/images-dir/', $text);
How can I write my regular expression so that it ignores any absolute URLs and only replaces the root relative URLs?