php code to replace the width and height of any html tag using php and regular expression.
i had a youtube listing for the admin where i display 10 per page but usually the object tag width and height will be around 400×350 approximately.
but i want to show the youtube video small in the admin section so that it will match the size of each row which will have edit, delete buttons…
here i wished the width and height to be around 100×80.
so when i display the youtube object tag code from the data base i used the following regular expression to replace the original width and height with my preferred value.
and here is the code to do the width and height replacement. it works for me and hope for you too…
$pattern = "/height=\"[0-9]*\"/"; $string = preg_replace($pattern, "height='120'", $rs['url']); $pattern = "/width=\"[0-9]*\"/"; $string = preg_replace($pattern, "width='200'", $string); echo $string;
we have another alternate for the above …
you can write the above code in one line by using alternation
$pattern = '/(width|height)="[0-9]*"/i'; //
you can use single-quotes instead of double-quotes to reduce the need to escape characters like double-quotes and backslashes.
the input was like this…
<object width="425" height="344"> <param name="movie" value="http://www.youtube.com/v/GwQMnpUsj8I&hl=en&fs=1"> </param><param name="allowFullScreen" value="true"> </param><param name="allowscriptaccess" value="always"> </param><embed src=http://www.youtube.com/v/GwQMnpUsj8I&hl=en&fs=1 type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"> </embed></object>
and the output will be like this…
<object width="200" height="120"> <param name="movie" value="http://www.youtube.com/v/GwQMnpUsj8I&hl=en&fs=1"> </param><param name="allowFullScreen" value="true"> </param><param name="allowscriptaccess" value="always"> </param><embed src=http://www.youtube.com/v/GwQMnpUsj8I&hl=en&fs=1 type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="200" height="120"> </embed></object>
there are two width and height attributes which got replaced… one is in the first line and next is for the embed tab inside.
…
April 22nd, 2010 at 1:37 am
I really love this script. I’m not able to get it to work though.
I’m using Wordpress and building a custom theme.
How do I run this script on the embed code?
Here is my code and the “get_post_meta” field calls my embed code.
ID, "video", $single = true); ?>
April 23rd, 2010 at 9:33 pm
Your code was too little to guess what you were going through…
There wasn’t anything to confuse… give another shot…
June 18th, 2010 at 12:33 pm
[...] … How to replace the width and height of an embed code or youtube video [...]