Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 5 votes, 2.80 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 12-21-2005, 09:30 PM   PM User | #2
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by Error 404
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.
Indeed, this is very useful for that. Its also good for script like that one topsite (Gold something? I don't remember the name.) that stored urls all broken up by parse_url() (For some pointless reason.) And they manually put the URLs back together for redirection, but this would reduce their code alot by using a simple, useful function. Thanks for this.
Element is offline   Reply With Quote
Old 12-26-2005, 04:10 PM   PM User | #3
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
New version:
PHP Code:
function cleanup_url($url)
{
    
$parsed parse_url($url);
    
$output '';
    if (isset(
$parsed['scheme']))
        
$output .= "$parsed[scheme]://";
    else
        
$output .= 'http://';
    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]";
    else
        
$output .= '/';
    if (isset(
$parsed['query']))
        
$output .= '?' urlencode(urldecode($parsed['query']));
    if (isset(
$parsed['fragment']))
        
$output .= "#$parsed[fragment]";
    if (isset(
$parsed['scheme']))
        return 
$output;
    else
        return 
cleanup_url($output);

What's new:
  • If no scheme is specified (eg. http, https, ftp) "http" is assumed
  • If no path is specified (eg. /newreply.php) "/" is assumed
__________________
Geoffrey Sneddon
gsnedders is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:39 AM.


Advertisement
Log in to turn off these ads.