replace width and height of html element using regular expression

PHP Add comments

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.

8 Responses to “replace width and height of html element using regular expression”

  1. John MacMenamin Says:

    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); ?>

  2. admin Says:

    Your code was too little to guess what you were going through…
    There wasn’t anything to confuse… give another shot…

  3. sathi Says:

    Thank you very much This helped me Alot,I saved my time,really thank you very much

  4. Gejo Says:

    Your code saved me a lot of time.
    Clean and elegant. Thanks!

  5. Jef Says:

    Thanks man, this was exactly what I was looking for!
    Really great script! :)

  6. Vinodkumar Saravana Says:

    this is great pattern…thanks man…

    But i have one doubt will it work if i have below content

    ………

    9940174183

  7. Vinodkumar Saravana Says:

    this is great pattern…thanks man…

    But i have one doubt will it work if i have below

    width=”120px” height=”150px”

  8. admin Says:

    Both worked for me now…

    $pattern = “/height=\”[0-9]*px\”/”;
    $pattern = “/width=\”[0-9]*px\”/”;

    $pattern = “/height=\”.*\”/”;
    $pattern = “/width=\”.*\”/”;

Leave a Reply

Entries RSS