PHP Explode Function Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
prolinu
Forum Newbie
Posts: 3
Joined: Sun Jul 05, 2009 8:57 am

PHP Explode Function Help

Post 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.
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: PHP Explode Function Help

Post 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
prolinu
Forum Newbie
Posts: 3
Joined: Sun Jul 05, 2009 8:57 am

Re: PHP Explode Function Help

Post 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?
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: PHP Explode Function Help

Post 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.
prolinu
Forum Newbie
Posts: 3
Joined: Sun Jul 05, 2009 8:57 am

Re: PHP Explode Function Help

Post 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
Post Reply