Miomir Dancevic
Miomir Dancevic

Reputation: 6852

Get some value from url?

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?

Upvotes: 0

Views: 176

Answers (1)

Hanky Panky
Hanky Panky

Reputation: 46900

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);
?>

Upvotes: 3

Related Questions