[eluser]ntheorist[/eluser]
thought i'd toss out this little function i added to the url helper. I run into problems sometimes when creating urls dynamically, in which concatenated urls end up with multiple slashes '//'. Hard sometimes to track where the slashes are so this function seems to help.
Optionally you can force prep_url, or if the url is already prepped it will re-prep it, to ensure the double slashes in http:// aren't replaced.
Code:
if ( ! function_exists('fix_slashes') )
{
function fix_slashes($str = NULL, $prep = FALSE)
{
if($str)
{
if (substr($str, 0, 7) == 'http://' || substr($str, 0, 8) == 'https://')
{
$prep = TRUE;
}
$str = str_replace('https://','',$str);
$str = str_replace('http://','',$str);
$str = preg_replace('|(/)+|','/',$str);
if($prep === TRUE)
{
$str = prep_url($str);
}
return $str;
}
return '';
}
}
usage:
$url = 'controller//function/var1//var2/';
fix_slashes($url); // controller/function/var1/var2/
fix_slashes($url, TRUE); // http://controller/function/var1/var2/
$url = 'http://mysite.com//controller/function/var1///var2//';
fix_slashes($url); // http://mysite.com/controller/function/var1/var2/
CC