1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Look at this code! Please help!

Discussion in 'PHP' started by netpox, Feb 18, 2008.

  1. #1
    On my site the code below takes any images posted by a user that is over 360px in width and resizes it so the max width stays at 360px, it then puts a link on the bottom of the image clickable to view original size.

    When an image is posted by a user that is like this:

    it works just fine but when users post it like this:

    it does not resize the image. It looks like anytime there is no quotes " or ' it does not work. Can someone look through the code and fix this so even if you don't enter the quotes it will still resize the image.

     
    netpox, Feb 18, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The problem lies here (in bold)...

    preg_match_all("/<img.*?src=(\"|')([^']*?)(\"|').*?>/", $row[comment], $matches);

    You're only checking ones that use ' or ", you'll have to either remove that or throw a ? after the ). As for the 2nd time it tries to find the ' or ".. you'll have to change that one to ( )?

    Here's what I'd change the top half of that code to (ending right before you set $image[tag]:
    #Image search
    
    preg_match_all("/<img.*?src=(?:\"|')?([^ >'\"]+)(?:[^>]*)>/i", $row[comment], $matches);
    
    #[0] Contains the image tags, [1] Contains the urls
    
    
    
    foreach($matches[0] as $key=>$value){
      $regexw = "/width=(?:\"|')?([0-9]+)/i";
      $regexh = "/height=(?:\"|')?([0-9]+)/i";
      
      preg_match_all($regexw,$value, $width);
      preg_match_all($regexh,$value, $height);
      
      $img[src] = $matches[1][$key];
      $img[width] = $width[1][0];
      $img[height] = $height[1][0];
    PHP:
    First, it checks if it does have ' or " and if it doesn't, says oh well. Also, I put ?: in the front of most of my ( ) which means to not add it into the array of matches (since it's not needed). It then checks the image's source by grabbing everything until it hits ', ", a space, or >. It then continues grabbing the rest of the tag.

    I did the same thing with your width & height grabbers, made them not care if there's ' or ". You really didn't need anything else after grabbing [0-9] from the width/height so I stopped checking there.
     
    zerxer, Feb 18, 2008 IP
    netpox likes this.
  3. netpox

    netpox Active Member

    Messages:
    1,625
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Awesome! That is exactly what i needed. I tested it and it works great! Thank you so much!
     
    netpox, Feb 19, 2008 IP