1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Preg match all

Discussion in 'PHP & Perl' started by rankdominator, Feb 28, 2010.

  1. rankdominator

    rankdominator Junior Member

    Joined:
    Dec 18, 2009
    Messages:
    117
    Likes Received:
    67
    Hi,

    If anybody can assist that would be appreciated.

    Ive got some text which Im trying to parse and strip out the images, but I do not want to strip out images that are only height=1 pixel or width =1 pixel

    This is the code at the moment

    PHP:
    preg_match_all('/<img(.+?)src=\"(.+?)\"(.*?)>/'$text$out);
    and it strips out everything - i mean all images. I only want images greater than a certain size


    Im not an expert on preg_match - thats for sure.
     
  2. IamNomad

    IamNomad Junior Member

    Joined:
    Mar 27, 2009
    Messages:
    135
    Likes Received:
    22
    Location:
    ┌∩┐(◕_◕)┌∩┐
    the code above is doing just as you described since youre not giving it any delimiter to indicate size. you need to indicate a match for width="1" height="1" as well. however, this is not a requirement in html4 or 5 to be valid. so if its not on the page.. youll need to figure out a way to get the image height without depending on the page itself.
     
  3. rankdominator

    rankdominator Junior Member

    Joined:
    Dec 18, 2009
    Messages:
    117
    Likes Received:
    67
    All the images have width and height attributes so that I suppose is good . How would I then go about adding a filter to the above preg match to only match where the height was for example greater than 10
    Posted via Mobile Device
     
  4. SafeSEO

    SafeSEO Jr. VIP Jr. VIP Premium Member

    Joined:
    Jan 10, 2010
    Messages:
    100
    Likes Received:
    6
    put here example line
     
  5. aмillionaírе

    aмillionaírе Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 20, 2008
    Messages:
    532
    Likes Received:
    358
    first of all add the height and weight attributes into the regular expression, using a \d+

    PHP:
    foreach($out[0] as $img){
    if(
    preg_match(<='1'$img)) { unset($img); }
      };
    P.S. - I haven't / couldn't have test this code, you haven't provided an applicable example/regex.
     
  6. rankdominator

    rankdominator Junior Member

    Joined:
    Dec 18, 2009
    Messages:
    117
    Likes Received:
    67
    Hi all,

    I came right by removing all the images with height =1 and width =1 first, then parsed the remaining text and got the remaining images ...

    Thanks for all the suggestions