Jump to content

Can someone explain why this does not work


affordit
Go to solution Solved by Psycho,

Recommended Posts

I am trying to allow youtube embed cods on my site but need to change the size if it does not fit. This is the code I am trying use, can someone tell me if there is a better way, this does not work.

<?php

$s = '<iframe width="640" height="420" src="http://www.youtube.com/embed/_LtZGQ65RNM" frameborder="5px" allowfullscreen></iframe>';

$s = str_replace('"', '\'', $s);

$s = explode(' ', $s);

print $s[1];

if($s[1]!== ("width='540'")){
str_replace($s[1], "width='540'", $s[1]);
}

if($s[2]!==("height='320'")){
str_replace($s[2], "height='320'", $s[2]);
}

print_r($s);

?>
Link to comment
Share on other sites

The reason it is not working is how you are using str_repolace

str_replace($s[1], "width='540'", $s[1]);

str_replace() RETURNS a modified value. You aren't doing anything with the returned value. You should have assigned it to $s[1]. But, you don't even need to use str_replace() since you want to replace the whole value. Just do this

if($s[1]!== ("width='540'"))
{
    $s[1] = "width='540'";
}

EDIT: Heck, you don't even need the if() statements - just set $s[1] and $s[2] to the requisite values

$s = '<iframe width="640" height="420" src="http://www.youtube.com/embed/_LtZGQ65RNM" frameborder="5px" allowfullscreen></iframe>';
$s = str_replace('"', '\'', $s);
$s = explode(' ', $s);

$s[1] = "width='540'";
$s[2] = "height='320'";

$s = implode(' ', $s);

But, I would probably implement a RexEx solution anyway since it won't matter where the height/width tags are in the string.

Edited by Psycho
Link to comment
Share on other sites

Nevermind I got it I used this:

<?php

$s = '<iframe width="640" height="420" src="http://www.youtube.com/embed/_LtZGQ65RNM" frameborder="5px" allowfullscreen></iframe>';

$s = str_replace('"', '\'', $s);

$s = explode('src', $s);

if($s[0]!== ("<iframe width='540' height='320' ")){
str_replace($s[0], "<iframe width='540' height='320' ", $s[0]);
}

$s = implode("src", $s);

print $s;

?>

Thanks for your help!

Link to comment
Share on other sites

That won't work if the tags are not in the order you expect. This will work better. With this it doesn't matter where the height/width tags are located in the string and it won't matter if they use single or double quotes.

 

 

$s = '<iframe width="640" height="420" src="http://www.youtube.com/embed/_LtZGQ65RNM" frameborder="5px" allowfullscreen></iframe>';
$s = preg_replace("#width=[\"']\d*[\"']#", 'width="540"', $s);
$s = preg_replace("#height=[\"']\d*[\"']#", 'height="320"', $s);
Link to comment
Share on other sites

  • Solution

OK, here's one last improvement. If the input string does not have height/width tags, then they obviously can't be replaced. This will resolve that

 

$width = 'width="540"';
$height = 'height="320"';
//Replace height/width tags with correct values
$s = '<iframe src="http://www.youtube.com/embed/_LtZGQ65RNM" frameborder="5px" allowfullscreen></iframe>';
$s = preg_replace("#width=[\"']\d*[\"']#", $width, $s);
$s = preg_replace("#height=[\"']\d*[\"']#", $height, $s);
//If no height/width tags exist add them
$firstSpace = strpos($s, ' ');
if(!strpos($s, $width))
{
    $s = preg_replace("# #", " $width ", $s, 1);
}
if(!strpos($s, $height))
{
    $s = preg_replace("# #", " $height ", $s, 1);
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.