troubleshooting str_replace

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
ebbatten
Forum Newbie
Posts: 11
Joined: Thu May 18, 2006 2:20 pm

troubleshooting str_replace

Post 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]
Paw
Forum Newbie
Posts: 20
Joined: Tue Jul 17, 2007 10:27 am

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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.
ebbatten
Forum Newbie
Posts: 11
Joined: Thu May 18, 2006 2:20 pm

Post by ebbatten »

Thanks guys!
Post Reply