How to modify my php regular code?

Discussion in 'Web Design and Development' started by youlichika, Sep 3, 2010.

  1. youlichika macrumors member

    Joined:
    Aug 27, 2010
    #1
    I would like to adjust some pictures size. I used this code:

    <?php
    preg_match_all('/<img[^>]*width=\"[1-9]\d{2}\" height=\"[1-9]\d{2}\"[^>]*\/>/is', $s, $matches);
    $firstimg = $matches[0][0];
    $newimg = preg_replace('/width=\"\d+\" height=\"\d+\"/', 'width="300" height="200"', $firstimg);
    echo $newimg;
    ?>

    But it can not show all the pictures, when the picture rule is <img src="" alt="" title="" /> There is no width and height, it can not work,
    how to add a width and height to these kind of pictures?
    Thanks.
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    I believe you're asking how to handle the case when the img does not include the height and width attributes. If so, this will help.

    PHP:
    preg_match_all('/<img[^>]*width=\"[1-9]\d{2}\" height=\"[1-9]\d{2}\"[^>]*\/>/is'$s$matches);
    $firstimg $matches[0][0];
    // Remove width / height
    $newimg preg_replace('/width=\"\d+\" height=\"\d+\"/'''$firstimg);
    // Add new height width
    $newimg preg_replace('/\/>/''width="300" height="200" \/>'$newimg);
    echo 
    $newimg;
     
  3. youlichika thread starter macrumors member

    Joined:
    Aug 27, 2010
    #3
    Thanks angelwatt, I am a newer for PHP regular. This is what I mean: how to handle the case when the img does not include the height and width attributes.

    I have tryed your code, but it not worked, can you check it for me?
    I know you first cancel all the 'width' and 'height' attributes, then add the new for them. This is a clever way.

    Or, I have another method.(maybe it can) Can anyone help me to finish a "if and else"

    if code
    Code:
    <?php
    preg_match_all('/<img[^>]*width=\"[1-9]\d{2}\" height=\"[1-9]\d{2}\"[^>]*\/>/is', $s, $matches);
    $firstimg = $matches[0][0];
    $newimg = preg_replace('/width=\"\d+\" height=\"\d+\"/', 'width="300" height="200"', $firstimg);
    echo $newimg;
    ?>
    
    else code
    Code:
    preg_match_all('#<img[^>]*>#i', $s, $matches)
    $test2=$matches[0][0];
    $test= array('<img'=>'<img width="300px" height="200px"');
    $newimg= strtr($test2,$test);}
    
    Waiting for the two method, thanks again.
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    Well you don't define $s so it's hard to say why it's not working for you, but it worked fine for me. Here's my complete test file.

    PHP:
    <pre>
    <?php
    $s 
    '<img src="image.jpg" width="100" height="100" alt="" />';
    preg_match_all('/<img[^>]*width=\"[1-9]\d{2}\" height=\"[1-9]\d{2}\"[^>]*\/>/is'$s$matches);
    $firstimg $matches[0][0];
    $newimg preg_replace('/width=\"\d+\" height=\"\d+\"/'''$firstimg);
    echo 
    htmlentities($newimg), "\n";
    $newimg preg_replace('/\/>/''width="300" height="200" \/>'$newimg);
    echo 
    htmlentities($newimg);
    ?>
    </pre>
     
  5. whatsgooddan macrumors member

    Joined:
    Apr 6, 2009
    Location:
    NY, USA
    #5
    You need some kind of database, or array, to store the height of each image plus the URL.
     
  6. youlichika thread starter macrumors member

    Joined:
    Aug 27, 2010
    #6
    Thanks, angelwatt. But when I use the img as below, it can not work.

    Code:
    <pre> 
    <?php 
    $s = '<img hspace="4" vspace="4" border="1" alt="Nagoya Institute folding X-Frame car lacks S-foils, hyperdrive, rolls on a big orange ball" src="http://www.blogcdn.com/www.engadget.com/media/2010/09/x-frame-2010-09-02-600.jpg" />'; 
    preg_match_all('/<img[^>]*width=\"[1-9]\d{2}\" height=\"[1-9]\d{2}\"[^>]*\/>/is', $s, $matches); 
    $firstimg = $matches[0][0]; 
    $newimg = preg_replace('/width=\"\d+\" height=\"\d+\"/', '', $firstimg); 
    echo htmlentities($newimg), "\n"; 
    $newimg = preg_replace('/\/>/', 'width="300" height="200" \/>', $newimg); 
    echo htmlentities($newimg); 
    ?> 
    </pre> 
    
     
  7. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #7
    Again you're not paying attention to what the thread is about. No databases needed here.

    We just need to change the match to simply catch all images, then process them. I also added a for loop to allow processing multiple matches.
    PHP:
    <pre>
    <?php
    $s 
    '<img hspace="4" vspace="4" border="1" alt="Nagoya Institute folding X-Frame car lacks S-foils, hyperdrive, rolls on a big orange ball" src="http://www.blogcdn.com/www.engadget.com/media/2010/09/x-frame-2010-09-02-600.jpg" />
    <img src="image.jpg" width="100" height="100" alt="" />'

    preg_match_all('/<img[^>]*?\/>/is'$s$matches);
    $firstimg $matches[0][0];

    for (
    $a=0$a count($matches[0]); $a++)
    {
        
    $newimg preg_replace('/ width="\d+" height="\d+"/'''$matches[0][$a]);
        echo 
    htmlentities(preg_replace('/\/>/''width="300" height="200" />'$newimg)), "\n";
    }
    ?>
    </pre>
     
  8. youlichika thread starter macrumors member

    Joined:
    Aug 27, 2010

Share This Page