affordit Posted June 26, 2013 Share Posted June 26, 2013 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); ?> Quote Link to comment Share on other sites More sharing options...
dalecosp Posted June 26, 2013 Share Posted June 26, 2013 It works fine here ... if I want to see an array in place of the tag. Are you sure you don't want to use preg_replace() instead? Quote Link to comment Share on other sites More sharing options...
affordit Posted June 26, 2013 Author Share Posted June 26, 2013 I see I got it right but how do I put the string back together? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 26, 2013 Share Posted June 26, 2013 (edited) 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 June 26, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
affordit Posted June 26, 2013 Author Share Posted June 26, 2013 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! Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 26, 2013 Share Posted June 26, 2013 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); Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted June 26, 2013 Solution Share Posted June 26, 2013 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); } Quote Link to comment Share on other sites More sharing options...
affordit Posted June 26, 2013 Author Share Posted June 26, 2013 Thanks Psycho that is amazingly simple. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.