views:

104

answers:

1

I am trying to use the youtube API to pulldown some videos for my site. Currently am running this code here:

//Youtube Videos Pull Down
$youtubeURL = "http://gdata.youtube.com/feeds/api/videos?alt=json&q=cats+cradle+chapel+hill&orderby=published&max-results=10&v=2";
$youtubeSearch = file_get_contents($youtubeURL, true);
$youtubeArray = json_decode($youtubeSearch, true);

Not having any problems accessing certain elements of the associative array however youtube's api is putting $ in many of its array elements .. such as [media$group]

Anytime I try to access an array with one of the $ elements in it, it doesn't work. Suggestions?

I have tried preg_replace but can't seem to get my expression right.

+4  A: 

You should be able to access it just fine, you just need to make sure to use single quotes or else php will try to interpolate $group as a variable, so:
$youtubeArray['media$group']

And if you want to use it in preg_replace, you have to escape it with a backslash: \$. $ is a valid regex identifier, so the regex is getting tripped up on it.

If you do replace it though, you should use str_replace. There is no need to bring (slower) regular expressions into this.

ryeguy
+1 Very well said!
Dan Heberden
Ahh single quotes did the trick. Thank you guys!
Chase
+1 you can never have enough single quotes. There's really no reason to ever use double quotes in PHP-- single quotes with concatenation are faster to parse and will be easier to read for most developers who are used to languages that only offer concatenation. The one exception is if you need to parse escaped characters like \n, where using double quotes is better than actually inserting a line break into your code.
Daniel