Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts

    preg_replace width and height wildcard in string

    I am trying to wrap my head around the preg_replace thing, but it is a little confusing to me. I need to replace the width and height in an string but it needs to be a wildcard. I have it working for the width, but when I do the same thing with the height for some reason it doesn't work. It just outputs the height at the original value. Any help would be greatly appreciated.

    PHP Code:
    <?php

    $string 
    '<object width="580" height="360">';

    $patterns '/\width=".*?"/';

    $replacements 'width="250"';

    $newstring preg_replace($patterns$replacements$string);

    $patterns '/\height=".*?"/';

    $replacements 'height="200"';

    $finalstring preg_replace($patterns$replacements$newstring);

    echo 
    $finalstring;

    ?>
    This is echoing <object width="250" height="360"> so it replaces the width, but not the height.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Do not escape w and h in the beginning of your patterns. Then it will work (for this string of course - you you would like to have more complex replacement, e.g. to replace if spaces could be present before and after "=" or for some other cases, please ask questions):
    PHP Code:
    <?php

    $string 
    '<object width="580" height="360">';

    $patterns '/width=".*?"/';

    $replacements 'width="250"';

    $newstring preg_replace($patterns$replacements$string);

    $patterns '/height=".*?"/';

    $replacements 'height="200"';

    $finalstring preg_replace($patterns$replacements$newstring);

    echo 
    $finalstring;

    ?>
    Last edited by SKDevelopment; 02-04-2010 at 01:05 PM.

  • Users who have thanked SKDevelopment for this post:

    dprichard (02-04-2010)

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Duh, awesome, thanks!!

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    One thing you should note is that it will replace all widths and heights if you are using this on a whole html file instead of just a single line. You could do this which will replace all widths and heights in one simple regex for any object as you described
    PHP Code:
    $string '<object width="580" height="360">';

    $pattern '/<object width="[^"]+" height="[^"]+">/i';
    $replace '<object width="250" height="200">';

    $finalstring preg_replace($pattern$replace$string);
    echo 
    $finalstring
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    90
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Thumbs up

    Hi,

    Here is another one line solution:
    PHP Code:
    $string '<object width="580" height="360">';
    echo 
    preg_replace("#(width|height)=[\'\"](\d)+[\'\"]#ies","(strtolower('\\1')=='width') ? \"\\1='280'\":\"\\1='200'\"",$string)."\r\n"
    Regards,
    Nick


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •