1

So I've got this RSS file that I'm trying to get part of a URL from. So here's what I tried (which is not working).

I've got this URL I can get easily enough:

http://www.youtube.com/watch?v=tUPjxGmh9i8&feature=youtube_gdata

I tried doing an $link = ltrim($link, 'http://www.youtube.com/watch?v='); in PHP and an $link = rtrim($trim, '&feature=youtube_gdata');

And it returned "UPjxGmh9i8". It cuts off the "t" in the front. The PHP.net documentation pages aren't the easiest for me to read and interpret, but I'm assuming that any individual character within the second parameter of ltrim() and rtrim() is taken out and this is not what I want. Is there some other solution I can use to grab only the text I want?

Any help is greatly appreciated.

Kevin Beal
  • 10,500
  • 12
  • 66
  • 92

2 Answers2

5

This is how I would do it...

$query = parse_url(
            'http://www.youtube.com/watch?v=tUPjxGmh9i8&feature=youtube_gdata', 
            PHP_URL_QUERY);

parse_str($query, $params);

$slug = get_magic_quotes_gpc() ? stripslashes($params['v']) : $params['v'];

CodePad.

The reason trim() (or its variants) won't work is because it accepts a character list (ordinal not significant), of which http://www.youtube.com/watch?v= contains t. It does not accept a string to remove.

alex
  • 479,566
  • 201
  • 878
  • 984
  • I had a chuckle with the magic quotes check. Let them rest in peace, amen. :) – Jon Dec 05 '11 at 02:35
  • @Jon: The `parse_str()` definitely feels like a hack; seems they just exposed the code that parses that text internally. – alex Dec 05 '11 at 02:36
  • @deceze: You may want to read the [relevant documentation](http://php.net/manual/en/function.parse-str.php). – alex Dec 05 '11 at 02:37
0

Using PHP, cant you just grab the value of v?

$result = $_GET['v'];
var_dump($result);
alex
  • 479,566
  • 201
  • 878
  • 984
jamesTheProgrammer
  • 1,747
  • 4
  • 22
  • 34
  • 1
    That will work if the OP is working on the YouTube site :P – alex Dec 05 '11 at 02:40
  • Maybe, I don't know how though. I'm getting this URL from an array I created from an XML file and I'm not actually going to the URL, just grabbing a segment of it. – Kevin Beal Dec 05 '11 at 02:42