You tube height and width changing

hi all

I have a you tube script like this

<object width=“425” height=“344”><param name=“movie” value=“http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&”></param><param name=“allowFullScreen” value=“true”></param><param name=“allowscriptaccess” value=“always”></param><embed src=“http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&” type=“application/x-shockwave-flash” allowscriptaccess=“always” allowfullscreen=“true” width=“425” height=“344”></embed></object>

I am storing this in my mysql databse.I need to show this in a page.I just passed this into a vriable and displaying ,this is fine.But I need to change the player height and width .How can I do this.

for eg: in youtube script width=“425” and height=“344”,But I need to change to width=“225” and height=“144” and it should be displayed.Basically I need to change the width and height,Itried htmlentities and addslases but I am not able to find a solution.please help me.

Please post the code that is not working, for further verification.

Try this:

$youTube = str_replace("width=\\"425\\"", "width=\\"225\\"", "$youTube");
$youTube = str_replace("width=\\"344\\"", "width=\\"144\\"", "$youTube");

http://php.net/manual/en/function.str-replace.php

I’m sure there’s a more sophisticated way to handle this with a single function but this should do the trick.

thanks

I tried this but notworking…

<?php
$youTube=“<object width=“425” height=“344”><param name=“movie” value=“http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&”></param><param name=“allowFullScreen” value=“true”></param><param name=“allowscriptaccess” value=“always”></param><embed src=“http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&” type=“application/x-shockwave-flash” allowscriptaccess=“always” allowfullscreen=“true” width=“425” height=“344”></embed></object>”;
$youTube = str_replace(“width=\“344\””, “width=\“144\””, “$youTube”);
echo $youTube;
?>

Parse error: syntax error, unexpected T_LNUMBER in C:\xampplite\htdocs\check\app\views\home\index.ctp on line 2

You’ll need to escape the double quotes in your variable.

$youTube = “<object width=\”“500\”“></object>”;

You place a backslash \ before the double quote to escape it. As you have quite a few slashes to escape here you could use addslashes();

$youTube = addslashes($youTube);
$youTube = str_replace(“width=\“344\””, “width=\“144\””, “$youTube”);
echo(“$youTube”);

not working this way also

<?php
$youTube=“<object width=“425” height=“344”><param name=“movie” value=“http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&”></param><param name=“allowFullScreen” value=“true”></param><param name=“allowscriptaccess” value=“always”></param><embed src=“http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&” type=“application/x-shockwave-flash” allowscriptaccess=“always” allowfullscreen=“true” width=“425” height=“344”></embed></object>”;
$youTube = addslashes($youTube);
$youTube = str_replace(“width=\“344\””, “width=\“144\””, “$youTube”);
echo(“$youTube”);
?>

You’re still not escaping the quotes in your first variable, $youTube. Notice how you place double quotes around the entire code snippet. That means that if you use double quotes within the variable itself you’ll have to escape them with a slash. Alternatively, you could change all of the double quotes within the <object> to single quotes.

$youTube=addslashes("<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>");
$youTube = str_replace("width=\\"344\\"", "width=\\"144\\"", "$youTube");
$youTube = str_replace("height=\\"344\\"", "height=\\"144\\"", "$youTube");
echo("$youTube");

Untested, not sure if my syntax is correct. The point is that you can’t define a variable that has double quotes as part of the value and also use double quotes to define it. You’ll need to escape the internal quotes with a slash.

still same error and not working…

hi this is my code
<?php

$youTube=addslashes(“<object width=“425” height=“344”><param name=“movie” value=“http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&”></param><param name=“allowFullScreen” value=“true”></param><param name=“allowscriptaccess” value=“always”></param><embed src=“http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&” type=“application/x-shockwave-flash” allowscriptaccess=“always” allowfullscreen=“true” width=“425” height=“344”></embed></object>”);
$youTube = str_replace(“width=\“344\””, “width=\“144\””, “$youTube”);
$youTube = str_replace(“height=\“344\””, “height=\“144\””, “$youTube”);
echo(“$youTube”);

?>

its not working

The double quotes are causing the trouble.

You need to use single quotes for PHP strings, and double quotes for HTML attributes.

Here is you current first line for the youtube string


$youTube=addslashes("<object width="425" height="344">...

Use a single quote for the string specifier.


$youTube=addslashes('<object width="425" height="344">...

Likewise, use a single quote at the end of the string


... width="425" height="344"></embed></object>');

With the rest of the code, using single strings allows you to have cleaner code.

Instead of this:


$youTube = str_replace("width=\\"344\\"", "width=\\"144\\"", "$youTube");
$youTube = str_replace("width=\\"344\\"", "width=\\"144\\"", "$youTube");

You will have this:


$youTube = str_replace('width="344"', 'width="144"', $youTube);
$youTube = str_replace('height="344"', 'height="144"', $youTube);
echo("$youTube");

Do you see how the confusion about what is a PHP string and what are HTML attributes now completely disappears?

Edit:

And why is the replacement occurring anyway? You can easily edit the original $youtube string and manually change 344 to 144

But video is not showing
<?php

$incoming_data=addslashes(‘$paths_youtube’);

$youTube = str_replace(‘width=“344”’, ‘width=“144”’, $incoming_data);
$youTube = str_replace(‘height=“344”’, ‘height=“144”’, $youTube);
echo $youTube;
?>

here $paths_youtube is from database where I stored my youtube script.

Remove the quotes from ‘$paths_youtube’ (so there are no quotes at all), and gain some understanding about PHP String Types

I tried but video is not showing

What do you think that we need to know to take this troubleshooting further, so that we can help you get it going.

Is there no error being displayed?

If you view the source of the page, do you see any HTML being output?

this works


$u = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>';

$u = str_replace('width="425"', 'width="999"', $u);
$u = str_replace('height="344"', 'height="999"', $u);

echo $u;

if it doesn’t for you, then we need more info, error messages? anything show? view source is what?

hi
you have mentioned like this

$u = str_replace(‘width=“425”’, ‘width=“999”’, $u);
$u = str_replace(‘height=“344”’, ‘height=“999”’, $u);

width=425;but in my case it is not fixed any width can come form you tube.

that is the real issue

If that’s the case you’ll most likely need a regular expression to filter out the string you’re looking to replace. Sorry, regular expressions are definitely not my strong point.

http://php.net/manual/en/function.preg-match.php

Great to see it only took 17 posts to get to the real issue :rolleyes:

regex will solve this for you, here is a nice site to learn about them, and here is a nice [URL=“http://regex.larsolavtorvik.com/”]tool to play around with them.

E: oh, and here is the php library.

Thanks for the tool! I rarely find myself needing regular expressions but this builder tool will save me loads of frustration. :slight_smile: