Page 1 of 1

PHP Explode Function Help

Posted: Sun Jul 05, 2009 9:09 am
by prolinu
My Programmer used this code to strip out characters expect the youtube ID in order to create the thumbnail picture of youtube video.

For a youtube link like: src="http://www.youtube.com/v/XrL5YIsUQu0"

He used this code:

Code: Select all

$colony     = explode("src=",$videoembed);
            $colonized  = explode("/",$colony[1]);
            $colonizer  = explode("\"",$colonized[4]);
                        $coloni         = explode("&",$colonizer[4]);
            $thumb_name = "http://img.youtube.com/vi/".$colonizer[0]."/default.jpg";
So that thumbnail image URL result will be http://img.youtube.com/vi/XrL5YIsUQu0/default.jpg

Now Youtube has additional strings with src=link. I am not able to explode the addition strings by using above method.

Now Youtube link look like this: src="http://www.youtube.com/v/XrL5YIsUQu0&hl=en&fs=1&rel=0"

How to strip out &hl=en&fs=1&rel=0 after the youtube video ID by using same above method?

Now youtube thumbnail image link comes like this:
http://img.youtube.com/vi/XrL5YIsUQu0&hl=en&fs=1&rel=0/default.jpg

I want it as http://img.youtube.com/vi/XrL5YIsUQu0/default.jpg

Your help will be appreciated.

Re: PHP Explode Function Help

Posted: Sun Jul 05, 2009 9:15 am
by ben.artiss
Hi prolinu,

Perhaps a bit of a simple way would be to use the str_replace() function:

Code: Select all

$url = str_replace('&hl=en&fs=1&rel=0', '', $origurl);
I'm no good at regular expressions but someone else may suggest a better way of doing it! :)

Regards, Ben

Re: PHP Explode Function Help

Posted: Sun Jul 05, 2009 9:23 am
by prolinu
ben.artiss wrote:Hi prolinu,

Perhaps a bit of a simple way would be to use the str_replace() function:

Code: Select all

$url = str_replace('&hl=en&fs=1&rel=0', '', $origurl);
I'm no good at regular expressions but someone else may suggest a better way of doing it! :)

Regards, Ben
hi Ben, Can you please tell me how can I use this str_replace function with the above code?

Re: PHP Explode Function Help

Posted: Sun Jul 05, 2009 9:27 am
by ben.artiss
Sure, assuming $videoembed is src="http://www.youtube.com/v/XrL5YIsUQu0&hl=en&fs=1&rel=0" then you'd do:

Code: Select all

...
$videoembed = str_replace('&hl=en&fs=1&rel=0', '', $videoembed); // This just replaces the &hl.. string with nothing
$colony = explode("src=",$videoembed);
...
Hope that works.

Re: PHP Explode Function Help

Posted: Sun Jul 05, 2009 9:39 am
by prolinu
ben.artiss wrote:Sure, assuming $videoembed is src="http://www.youtube.com/v/XrL5YIsUQu0&hl=en&fs=1&rel=0" then you'd do:

Code: Select all

...
$videoembed = str_replace('&hl=en&fs=1&rel=0', '', $videoembed); // This just replaces the &hl.. string with nothing
$colony = explode("src=",$videoembed);
...
Hope that works.
Perfect! That worked.

Thank you very much