PHP parameters inside url cumulated parameter - php

lets say I have an url
http://www.somepage.com/clicker.php?id_campaign=8&id_email=9324&url=https://www.google.com/search?q=dog&sxsrf=ALeKk019eteEpAwVf2Fk4qYo7TiwhuMQ_Q:1596666153666&source=lnms&tbm=isch&sa=X&ved=2ahUKEwiqzqf3jIXrAhUisaQKHRayBWAQ_AUoAXoECBkQAw&biw=1920&bih=921#imgrc=M4wsJO0A7OQfTM
and im trying to get out the parameters id_campaign, id_email and url
however, using the code:
$url = htmlspecialchars_decode($url);
$parts = parse_url($url);
parse_str($parts['query'], $query);
when printing $parts['query'] it only gives me:
https://www.google.com/search?q=dog
because rest of the url it consider as another parameter...
so how do I get all the url parameter out of $url?

In an ideal scenario, the URLs passed within the query would be URL-encoded, as opposed to HTML-encoded.
What you could do in your case is manually replace all & by something temporary and then replace them all back after parsing the URL. This is not exactly pretty, but works:
$url = str_replace('&', '__TEMP__', $url);
$parts = parse_url($url);
parse_str($parts['query'], $query);
$urlParam = str_replace('__TEMP__', '&', $query['url']);
echo $urlParam;
Demo

Related

changing get variable using parse_url not working in php

I've looked at every post on SO that remotely pertains to this and I just can't figure this out. This code is taken directly from another SO post and was marked as the correct working answer:
$query = $_GET;
// replace parameter(s)
$query['d'] = 'new_value';
// rebuild url
$query_result = http_build_query($query);
// new link
Link
Again, taken straight from another post. When I try this code, i change the $_GET to the actual URL that i want to alter. When the code gets to the $query['d'] part, it tells me I get an illegal string offset and the error is the index that's specified. So then I parse the URL, and then do parse_str($query, $output) which in turn allows me to do $output['d'] and THEN I can set a new value to that variable. If I echo it out, it's fine.
But then I get to the http_build_query line, and it tells me that it's expecting an array or object and I can't build the new URL. Here is my code:
$link = parse_url('https://www.google.com/search?source=hp&ei=85GhW6CNHoSqsgXnzoD4Ag&q=coding+tutorial&btnK=Google+Search&oq=coding+tutorial', PHP_URL_QUERY);
parse_str($link, $output);
$output['oq'] = 'new value';
$query_result = http_build_query($link);
echo $query_result;
This code yields that the http_build_query function wants an array or object...i guess i'm not giving it that in some way? What do I need to do to get this to work?
If you want to rebuild the full URL after modifying the query parameters, you could do this:
$url = 'https://www.google.com/search?source=hp&ei=85GhW6CNHoSqsgXnzoD4Ag&q=coding+tutorial&btnK=Google+Search&oq=coding+tutorial';
$link = parse_url($url, PHP_URL_QUERY);
parse_str($link, $output);
$output['oq'] = 'new value';
echo substr($url, 0, strpos($url, '?') + 1) . http_build_query($output);
Output:
https://www.google.com/search?source=hp&ei=85GhW6CNHoSqsgXnzoD4Ag&q=coding+tutorial&btnK=Google+Search&oq=new+value

Replace url paramer by using php

I have a url & its structure like
https://www.example.com/i/location-name/category/subcategory/item/item-id
I want to replace the url parameter(location-name) dynamically by new parameter using PHP.
Modified url is looks like this
https://www.example.com/i/new-parameter/category/subcategory/item/item-id
I had successfully done with query parameters by using http_build_query();
But in this case i had tried with preg_replace(), but its not working
Thanks
If you would like to use regex.
$url = 'https://www.example.com/i/location-name/category/subcategory/item/item-id';
$new_param = 'new-parameter';
print preg_replace('|/location-name/|','/'.$new_param.'/',$url);
I don't recommend search for only location-name (without slash) beacause it will match with for example location-names string.
UPDATE
Based on placement not string you can change that part that way:
$url = 'https://www.example.com/i/location-name/category/subcategory/item/item-id';
$new_param = 'new-parameter';
$new_url = preg_replace('|/i/(.*?)/|','/i/'.$new_param.'/',$url);
print $new_url.'<br/>';
$new_param = 'another-parameter';
$new_url = preg_replace('|/i/(.*?)/|','/i/'.$new_param.'/',$url);
print $new_url.'<br/>';
You will get:
https://www.example.com/i/new-parameter/category/subcategory/item/item-id
https://www.example.com/i/another-parameter/category/subcategory/item/item-id
Alternative solution
If you would like to be sure about change, you can do it another way, something like this:
$url = 'https://www.example.com/i/location-name/category/subcategory/item/item-id';
$new_param = 'new-param';
$parts = parse_url($url);
$path_parts = explode('/',$parts['path']);
$path_parts[2] = $new_param;
$new_path = implode('/',$path_parts);
$new_url = $parts['scheme'].'://'.$parts['host'].$new_path;
print( $new_url);
Try this one:
$new_url = str_replace('location-name', 'my-location', 'https://www.example.com/i/location-name/category/subcategory/item/item-id')

I need to extract exact url using php including the id in the address

I need to extract following url using php : "http://www.website.com/profile#username". All the methods that I have tried return "http://www.website.com/profile"
Just use parse_url()
$url = 'http://www.website.com/profile#username';
$parse = parse_url($url);
print $parse['host']; // prints 'website.com'

PHP: str_replace within a word/phrase

I am trying to set up a small script that can play youtube videos but thats kinda besides the point.
I have $ytlink which equals www.youtube.com/watch?v=3WAOxKOmR90
But I want to make it become www.youtube.com/embed/3WAOxKOmR90
Currently I have tried
$result = str_replace('https://youtube.com/watch?v=', "https://youtube.com/watch?v=", $ytlink);
But this returns it as standard
I have also tried
preg_replace('/https://youtube.com/watch?v=/, '/https://youtube.com/embed/', $ytlink);
but both of these dont work.
Instead of using ugly regexes, I recommend using parse_url() with parse_str(). This allows you to be flexible in the event that you want to change something or if Youtube decides to change their URL slightly.
$url = 'https://www.youtube.com/watch?v=3WAOxKOmR90';
// Parse the URL into parts
$parsed_url = parse_url($url);
// Get the whole query string
$query = $parsed_url['query'];
// Parse the query string into parts
parse_str($query, $params);
// Get the parameter you want
$v = $params['v'];
// Now re-build the URL how you want
echo $parsed_url['scheme'].'://'.$parsed_url['host'].'/embed/'.$v;
// Outputs: https://www.youtube.com/embed/3WAOxKOmR90
This works:
$ytlink = 'www.youtube.com/watch?v=3WAOxKOmR90';
$result = str_replace('watch?v=', 'embed/', $ytlink);
echo $result;
$url = 'www.youtube.com/watch?v=3WAOxKOmR90';
echo preg_replace('/.*?v=(\w+)/i', 'www.youtube.com/embed/$1', $url);

Grab the video ID only from youtube's URLs

How can I grab the video ID only from the youtube's URLs?
For instance,
http://www.youtube.com/watch?v=aPm3QVKlBJg
sometime the URLs contain other information after the 'v' like
http://www.youtube.com/watch?v=Z29MkJdMKqs&feature=grec_index
but I don't want the other info, just video ID.
I only can think of using explode,
$url = "http://www.youtube.com/watch?v=aPm3QVKlBJg";
$pieces = explode("v=", $url);
but how to clean up the URLs like this?
http://www.youtube.com/watch?v=Z29MkJdMKqs&feature=grec_index
You should never use regular expressions when the same thing can be accomplished through purpose-built functions.
You can use parse_url to break the URL up into its segments, and parse_str to break the query string portion into a key/value array:
$url = 'http://www.youtube.com/watch?v=Z29MkJdMKqs&feature=grec_index'
// break the URL into its components
$parts = parse_url($url);
// $parts['query'] contains the query string: 'v=Z29MkJdMKqs&feature=grec_index'
// parse variables into key=>value array
$query = array();
parse_str($parts['query'], $query);
echo $query['v']; // Z29MkJdMKqs
echo $query['feature'] // grec_index
The alternate form of parse_str extracts variables into the current scope. You could build this into a function to find and return the v parameter:
// Returns null if video id doesn't exist in URL
function get_video_id($url) {
$parts = parse_url($url);
// Make sure $url had a query string
if (!array_key_exists('query', $parts))
return null;
parse_str($parts['query']);
// Return the 'v' parameter if it existed
return isset($v) ? $v : null;
}

Categories