Log in

View Full Version : Seperating data with PHP



Decode
14-09-2008, 03:55 PM
I'm trying to figure out how to separate the id from a youtube url, however I can't use explode because after the id in the url there is nothing to explode.

http://www.youtube.com/watch?v=Q4pY3QtiGyo

I need to separate the bit in bold, however someone might imput an url like this-

http://www.youtube.com/watch?v=Q4pY3QtiGyo&feature=rec-fresh

Which would make exploding it even harder, any ideas?

MrCraig
14-09-2008, 03:57 PM
<?php
$str = "http://www.youtube.com/watch?v=Q4pY3QtiGyo&feature=rec-fresh";
if(eregi("&",$str))
{
$str = explode("v=",$str);
$str = explode("&",$str[1]);
$str = $str[0];
}
else
{
$str = explode("v=",$str);
$str = $str[1];
}
?>


I think that should cover all cases :P

Decode
14-09-2008, 04:00 PM
Yeah, unfortunately that's the problem, the & sign isn't always there.

MrCraig
14-09-2008, 04:02 PM
Ye i just realised and edited post :P

Decode
14-09-2008, 04:11 PM
Thanks :)

Want to hide these adverts? Register an account for free!