Page 1 of 1

troubleshooting str_replace

Posted: Tue Jul 17, 2007 11:08 am
by ebbatten
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Can i get some fresh eyes to look over this:

Code: Select all

function displayYoutube($url, $width=250, $height=225)
{

$videoID = str_replace('http://www.youtube.com/watch?v=','',$url);
return '<object width="'.$width.'" height="'.$height.'">
            <param name="movie" value="http://www.youtube.com/v='.$videoID.'"></param>
            <param name="wmode" value="transparent"></param>
            <embed src="http://www.youtube.com/v='.$videoID.' "type="application/x-shockwave-flash" wmode="transparent" width="'.$width.'" height="'.$height.'"></embed>
        </object>';
}
When i look at the value that is returned in a view source, it says the following:

Code: Select all

<object width="250" height="225">
            <param name="movie" value="http://www.youtube.com/v=http://youtube.com/watch?v=8OZREnV6F0o"></param>
            <param name="wmode" value="transparent"></param>
            <embed src="http://www.youtube.com/v=http://youtube.com/watch?v=8OZREnV6F0o"type="application/x-shockwave-flash" wmode="transparent" width="250" height="225"></embed>
        </object>
what am i doing wrong????this is driving me cuh-razy. thanks guys.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jul 17, 2007 11:13 am
by Paw
Well, you're looking for the string http://www.youtube.com/v= to replace, but you're passing http://youtube.com/watch?v=8OZREnV6F0o as parameter "$url".
$url is obviously missing the www.-part, therefore it cannot be found, nor replaced.

Posted: Tue Jul 17, 2007 11:15 am
by Zoxive
Look closely at

Code: Select all

$videoID = str_replace('http://www.youtube.com/watch?v=','',$url); 
And what it isn't replacing

Code: Select all

<param name="movie" value="http://www.youtube.com/v=http://youtube.com/watch?v=8OZREnV6F0o">
You are replacing http://www.youtube.com, but not youtube.com, you should probably look into regex.

Posted: Tue Jul 17, 2007 11:36 am
by ebbatten
Thanks guys!