views:

38

answers:

2

I'm trying to trim some youtube URLs that I am reading in from a playlist. The first 3 work fine and all their URLs either end in caps or numbers but this one that ends in a lower case g is getting trimmed one character shorter than the rest.

    for ($z=0; $z <= 3; $z++)
  {
   $ythref2 = rtrim($tubeArray["feed"]["entry"][$z]["link"][0]["href"], '&feature=youtube_gdata');

The URL is http://www.youtube.com/watch?v=CuE88oVCVjg&amp;feature=youtube_gdata .. and it should get trimmed down to .. http://www.youtube.com/watch?v=CuE88oVCVjg but instead it is coming out as http://www.youtube.com/watch?v=CuE88oVCVj.

I think it may be the ampersand symbol but I am not sure.

+3  A: 

The second argument to rtrim is a list of characters to remove, not a string to remove.

You might want to use str_replace, or use parse_url and parse_str to get arrays of the components of the URL and the components of the query string, like "v".

Untested example code:

$youtube_url = 'http://www.youtube.com/watch?v=CuE88oVCVjg&amp;feature=youtube_gdata';
$url_bits = parse_url($youtube_url);
$query_string = array();
parse_str($url_bits['query'], $query_string);
$video_identifier = $query_string['v']; // "CuE88oVCVjg"
$rebuilt_url = 'http://www.youtube.com/watch?v=' . $video_identifier;
Charles
+1  A: 

No, it's the g in the second argument. rtrim() does not remove a string from the end, it removes any characters given in the second argument. Use preg_replace() or substr() instead.

Ignacio Vazquez-Abrams
Perfect, used substr. Thank you!
codeisforeva