relative naar absolute url’s
4 April 2007Ik was een functie aan het schrijven om relative urls om te zetten naar absolute. Bijvoorbeeld:
-
... href="/foo.php?bar=1" ...
moet worden vervangen door
-
... href="http://www.example.net/foo.php?bar=1" ...
Aangezien ik hier wel wat moeite mee gehad heb, deel ik graag de functie.
-
/**
-
* Convert relative urls into absolute urls
-
*
-
* @param string $prefix
-
* @param string $text
-
* @return string
-
*/
-
function relativeToAbsolute($prefix, $text)
-
{
-
// search for single quotes and replace them by double quotes
-
$search = '\'';
-
$replace = '"';
-
-
// replace relative urls by absolute (prefix them with $prefix)
-
$pattern = '/href="(?!http|https|ftp|irc|feed|mailto)([\/]?)(.*)"/i';
-
$replace = 'href="'.$prefix.'/$2"';
-
-
// return
-
return $text;
-
}




Recente reacties