I’m going to write the Php parse_url() function today; the parse_url() function is supported by PHP4 and later versions. With this function, it’s possible to split the address we want into parts like scheme, host, path, query. Recently, for a project, I needed to get the watch?v=* value of a YouTube video address; instead of classic string operations, I used the parse_url() function and really enjoyed it. Let’s move on to how to use the function now;
<?php $url = 'http://www.youtube.com/watch?v=RQieoqCLWDo&feature=relmfu'; $arr = parse_url($url); print_r($arr); ?>
Output: Array ( [scheme] => http [host] => www.youtube.com [path] => /watch [query] => v=RQieoqCLWDo&feature=relmfu )
will be. Now let’s look at how we can get the Query values;
<?php
$url = 'http://www.youtube.com/watch?=RQieoqCLWDo&feature=relmfu';
$parts = parse_url($url);
$query = array();
parse_str($parts['query'], $query);
echo "v= " . $query['v']."<br>";
echo "feature= " . $query['feature'];
?>
as we can see http://www.youtube.com/watch?v=RQieoqCLWDo&feature=relmfu
we have retrieved only the query values of the address; its output is;
v= RQieoqCLWDo
feature= relmfu
Good luck..

Share your experience
Got a question, a different solution or a note to add?
Log in or join us to write a comment.
Log in ↗︎Sign up ↗︎