Thread: URL Implode
View Single Post
Old 12-21-2005, 06:32 PM   PM User | #1
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
URL Implode

PHP Code:
function url_implode($parsed)
{
    
$output '';
    if (isset(
$parsed['scheme']))
        
$output .= "$parsed[scheme]://";
    if (isset(
$parsed['user']) && isset($parsed['pass']))
        
$output .= "$parsed[user]:$parsed[pass]@";
    elseif (isset(
$parsed['user']))
        
$output .= "$parsed[user]@";
    if (isset(
$parsed['host']))
        
$output .= rawurlencode(rawurldecode($parsed['host']));
    if (isset(
$parsed['path']))
        
$output .= "$parsed[path]";
    if (isset(
$parsed['query']))
        
$output .= '?' urlencode(urldecode($parsed['query']));
    if (isset(
$parsed['fragment']))
        
$output .= "#$parsed[fragment]";
    return 
$output;

This function will take an parse_url array, and return a URL.

You may be wondering what the point of it is, as (normally) if you have a parse_url array, you have the URL. I was creating a PHP script to grab news from a site (using preg_match_all()) and produce an Atom feed. An Atom feed is XML, therefore must be well-formed, so all the parts of a URL must be properly escaped.
__________________
Geoffrey Sneddon
gsnedders is offline   Reply With Quote