RSS doesn't know a base-URL

Writing about the base tag and stupid search engines I completly forgot that RSS doesn't accept relative links - for a good reason of course. This caused at least Bloglines to get a little bit wild about my feed and I'm sorry for the inconveniences this may have caused.

Anyhow, I decided to get rid of the problem once and for all by turning relative links into absolute when writing the feed. This is how I did it:

<?php function relToAbs($text, $base) {   if (empty($base))     return $text;   // base url needs trailing /   if (substr($base, -1, 1) != "/")     $base .= "/";   // Replace links   $pattern = "/<a([^>]*) " .              "href=\"[^http|ftp|https]([^\"]*)\"/";   $replace = "<a\${1} href=\"" . $base . "\${2}\"";   $text = preg_replace($pattern, $replace, $text);   // Replace images   $pattern = "/<img([^>]*) " .               "src=\"[^http|ftp|https]([^\"]*)\"/";   $replace = "<img\${1} src=\"" . $base . "\${2}\"";   $text = preg_replace($pattern, $replace, $text);   // Done   return $text; } ?>

Just a quick shot, but it seems to work.

Posted on November 4, 2005 in |


Comment by Anonymous

January 31, 2006 - 02:13

I noticed that I'm having the same problem.
Where did you use this code? Thanks!


Comment by Gerd Riesselmann

February 10, 2006 - 16:40

I use it within my TotalFeeds module :-)


Comment by Chris

January 16, 2007 - 00:10

Great solve, thanks!