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?
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.
$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:
... because introducing another function and another variable for nothing to an already working example is such a good idea?
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.
$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):