// Yazılım

PHP url parse

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..

// FEEDBACK

Was this useful?

Your rating shows how helpful this note is to other readers.

Was this useful?

Click the stars to rate.

Average rating 0 / 5. Total votes: 0

No one has rated this post yet. Be the first!

Sorry this post was not helpful.

Your feedback is valuable to us.

How can we make this post more helpful?

// JOIN THE CONVERSATION

Share your experience

Got a question, a different solution or a note to add?

// KEEP READING

One more note.

All posts ↗︎