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…
October 2nd, 2010 at 8:43 pm
Thank you very much This helped me Alot,I saved my time,really thank you very much
July 18th, 2011 at 11:13 pm
Your code saved me a lot of time.
Clean and elegant. Thanks!
June 1st, 2012 at 8:25 pm
Thanks man, this was exactly what I was looking for!
Really great script! 🙂
November 8th, 2012 at 12:56 pm
this is great pattern…thanks man…
But i have one doubt will it work if i have below content
………
9940174183
November 8th, 2012 at 12:57 pm
this is great pattern…thanks man…
But i have one doubt will it work if i have below
width=”120px” height=”150px”
November 8th, 2012 at 7:07 pm
Both worked for me now…
$pattern = “/height=\”[0-9]*px\”/”;
$pattern = “/width=\”[0-9]*px\”/”;
$pattern = “/height=\”.*\”/”;
$pattern = “/width=\”.*\”/”;