I have this kind of url from youtube, with the value of video
$url='http://www.youtube.com/watch?v=H_IkPia6eBA&';
What i need is just to get new string with value of V etc in this case
$newstring='H_IkPia6eBA&';
I dont know how long V could be, only i need to get that value of V, I have tried
$string = 'http://www.youtube.com/watch?v=oYyslNuRcwM';
$url = parse_url($string);
parse_str($url['query'], $query);
print_r($query);
Tried with this, but in CodeIgniter post, I only get empty array?
You're almost there already.
<?php
$string = 'http://www.youtube.com/watch?v=oYyslNuRcwM';
$url = parse_url($string);
parse_str($url['query'], $query);
$newstring=$query["v"]; // just this line is missing
echo $newstring;
?>
Demo
But you know something? If the url format is always going to be like that then no need for all those functions. It then can simply be
<?php
$url='http://www.youtube.com/watch?v=H_IkPia6eBA&';
echo str_replace("http://www.youtube.com/watch?v=","",$url);
?>
Related
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
I am trying to parse url and extract value from it .My url value is www.mysite.com/register/?referredby=admin. I want to get value admin from this url. For this, I have written following code. Its giving me value referredby=admin, but I want only admin as value. How Can I achieve this? Below is my code:
<?php
$url = $current_url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
setcookie('ref_by', parse_url($url, PHP_URL_QUERY));
echo $_COOKIE['ref_by'];
?>
You can use parse_str() function.
$url = "www.mysite.com/register/?email=admin";
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['email'];
Try this code,
$url = "www.mysite.com/register/?referredby=admin";
$parse = parse_url($url, PHP_URL_QUERY);
parse_str($parse, $output);
echo $output['referredby'];
$referred = $_GET['referredby'];
$referred = "referredby=admin";
$pieces = explode("=", $referred);
echo $pieces[1]; // admin
I don't know if it's still relevant for you, but maybe it is for others: I've recently released a composer package for parsing urls (https://www.crwlr.software/packages/url). Using that library you can do it like this:
$url = 'https://www.example.com/register/?referredby=admin';
$query = Crwlr\Url\Url::parse($url)->queryArray();
echo $query['referredby'];
The parse method parses the url and returns an object, the queryArray method returns the url query as array.
Is not a really clean solution, but you can try something like:
$url = "MYURL";
$parse = parse_url($url);
parse_str($parse['query']);
echo $referredby; // same name of the get param (see parse_str doc)
PHP.net: Warning
Using this function without the result parameter is highly DISCOURAGED and DEPRECATED as of PHP 7.2.
Dynamically setting variables in function's scope suffers from exactly same problems as register_globals.
Read section on security of Using Register Globals explaining why it is dangerous.
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);
I want to get the individual ID from a youtube link so that I can use it later on, to pull other information such as its thumbnail image and insert it into the embedded iframe.
I notice there that all, I hope, of the urls on youtube start http://www.youtube.com/watch?v= Unique video ID and then depending on a few things can end something like &list=RD02M5u5zXVmoHM. But the unique id always has a & after it and the watch?v= before. Is this the best way to do it? Have a written this correctly?
$link=$_POST['link'];
$link_id=str_replace('http://www.youtube.com/watch?v=', '', $link);
$link_id=strstr($link_id, '&', true);
This comes from form data. Is there anything other variables from youtube URLs I may encounter?
What you want to do is use: http://php.net/pase_url and http://php.net/parse_str like this:
<?php
$info = parse_url("https://www.youtube.com/watch?v=DA57FOJSdM8");
parse_str($info["query"], $query);
print_r($query);
// Outputs: Array ( [v] => DA57FOJSdM8 )
A combination of parse_url() and parse_str() would be a simpler way where PHP handles the dirty work for you. Example:
$video_id = false;
$url_parts = parse_url($link);
$query_params = array();
if (isset($url_parts['query']))
{
parse_str($url_parts['query'], $query_params);
}
if (isset($query_params['v']))
{
$video_id = $query_params['v'];
}
And now $video_id contains the ID you need.
I need to be able to remove a URL from a variable, I'm wondering how i do this.
Example - Say my script returns http://www.example.com/file.php?id=1234 i need to be able to remove the http://www.example.com/file.php?id= bit, just leaving the id number. If anyone can help, it would be great :)
Something like this?
$var = 'http://www.example.com/file.php?id=1234';
$query = parse_url($var, PHP_URL_QUERY);
$query_components = parse_str($query);
$id = $query_components['id'];
You can use regular expressions:
preg_match("/id=(\\d+)/", $url, $matches);
$id = $matches[1];
Just use $id = $_GET['id'];.
See the docs.
And don't forget to validate and sanitize.
The "id" in this case is being sent to your script as a GET variable, therefore you would access it as follows:
$id = $_GET['id'];
If you mean to say that this URL is not yours to control, then you would do this instead:
print_r(parse_url($url)); // Then analyze the output.