Peter M Howard ::

Converting Relative to Absolute links in PHP (preg_replace)

21Sep2005/16Sep2006 [webprog]

It took a while, but I managed to get my head around the Regex syntax to change relative links (in href or src attributes) to absolute ones (which I had to do for the RSS feed)... Requires the following line (in PHP):

$str=preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#','$1="http://wintermute.com.au/$2$3',$str);

Explanations, from left to right:

It took me a while to find any information like this on the internet so I'm posting this hint here. There's plenty on REGEX (a search on regex syntax will pick up lots), but I struggled to find anything on rewriting links in this context... It was also made more difficult by the timestamps using colons, but though ugly, the solution works elegantly.

Update: I'm seeing this entry get a lot of search engine hits, so in order that it might be a little more useful, I'm going to add some simplified code.

Still read the above section as it helps to get an idea of what's going on, BUT, if you know that the relative link you're rewriting has NO colons (:) in it, there's a simpler piece of code you can use:

$str=preg_replace('#(href|src)="([^:"]*)(?:")#','$1="http://wintermute.com.au/$2"',$str);

Of course, if your relative link MAY have colons in it, and you know nothing about their position in the string, both these codes are useless. Further, any quotes (") in a relative link MUST be encoded or the preg_replace string will stop processing. I have the whole end bit checking for spaces there because I KNOW that a space will occur before any colons in my own relative links. YMMV, and of course, I recommend testing the strings thoroughly before relying on it.

Update: Months later I've gone through and fixed my internal URIs to use a '+' instead of a space, which wasn't entirely proper; I updated the first string accordingly

« let’s go out tonight :: Search Terms half-working »

Related [webprog]