1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Encode part of url

Discussion in 'PHP' started by Fracisc, May 16, 2014.

  1. #1
    I have this fixed variable:
    $url= "http://site.com/click?ad=ss&aff=ss&redirect=http://www.site.com/navigation.do?action=ShowProduct&productId=75193";
    Code (markup):
    What I need to do is to grab the $url and encode ONLY the "redirect" variable. Basically all I need to be encoded is this part of the $url:
    "http://www.site.com/navigation.do?action=ShowProduct&productId=75193"
    Code (markup):
    How can I do that?
     
    Fracisc, May 16, 2014 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    By encode I'll assume you mean using urlencode -- Normally I'd consider using parse_url for that, but that actually might be getting too complex for it's own good when it comes time to glue it back together.

    If all you're trying to do is extract that one piece... and you are sure that &redirect= is always the last local parameter, I'd just explode it with a 2 limit.

    $redirect = explode('&redirect=', $url, 2);
    $newURL = $redirect[0] . '&redirect=' . urlencode($redirect[1]);

    That SHOULD do the trick.

    ... and yeah, passing URL's that have more than one query parameter AS a query parameter is a pain in the backside.
     
    deathshadow, May 16, 2014 IP
    Fracisc likes this.
  3. figooo

    figooo Active Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #3
    $url= "http://site.com/click?ad=ss&aff=ss&redirect=http://www.site.com/navigation.do?action=ShowProduct&productId=75193";
    list($basic_url,$redirect)=explode('&redirect=', $url, 2);
    $new_url=$basic_url.'&redirect='.urlencode($redirect);
    PHP:
     
    figooo, May 16, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    ... because introducing another function and another variable for nothing to an already working example is such a good idea?
     
    deathshadow, May 16, 2014 IP
  5. Jigney

    Jigney Active Member

    Messages:
    168
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    93
    #5
    Simple, First of all you have to get the substring, for that you can use explode function and After that you can encode that URL and then again make a new URL with that ecoded URL.
     
    Jigney, May 21, 2014 IP
  6. mxscripts

    mxscripts Well-Known Member

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    115
    Digital Goods:
    1
    #6
    $url= "http://site.com/click?ad=ss&aff=ss&redirect=http://www.site.com/navigation.do?action=ShowProduct&productId=75193";
    Code (markup):

    First, extract part of the url you need (http: // www. site.com/navigation.do?action=ShowProduct&productId=75193)
    
    $search = "http://site.com/click?ad=ss&aff=ss&redirect=");
    $redirect_url = str_replace($search, "", $url);
    
    Code (markup):
    Then encode $redirect_url:
    
    $redirect_url_encoded = urlencode($redirect_url);
    
    Code (markup):
    So, new url will be:
    
    $new_url = "http://site.com/click?ad=ss&aff=ss&redirect=".$redirect_url_encoded;
    
    Code (markup):
     
    mxscripts, May 27, 2014 IP