Preg_Match negating a string

Discussion in 'PHP' started by Shoe ButtBack, Mar 18, 2010.

  1. #1
    Hi,

    How would you negate a string in preg_match

    Say I want it to find everything in a string EXCEPT that containing "</" in it.

    I tried /([^(<\/)]*/ and /([^<\/]*/ but its not working.

    I understand [^<]* would parse anything not including "<" , but how can I do the same thing for 2 characters, being "</" in my case?

    Thanks in advance,
    Armin
     
    Shoe ButtBack, Mar 18, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    <?php
    
    $str = <<<eof
    test </tag>
    eof;
    
    preg_match('~([^</]*)~', $str, $arr);
    
    print_r($arr);
    
    ?>
    PHP:
     
    danx10, Mar 18, 2010 IP
  3. Shoe ButtBack

    Shoe ButtBack Active Member

    Messages:
    179
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    68
    #3
    Thanks for your answer but this is not what I want. This will match ignore things that have a "<" OR "/" in the string, but I ONLY want it to ignore strings with "</" in them. So the sequence "</" CANNOT appear, but < and / individually can.

    Any clues?
     
    Shoe ButtBack, Mar 18, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Can you post you an example, input and expected output.
     
    danx10, Mar 18, 2010 IP
  5. Shoe ButtBack

    Shoe ButtBack Active Member

    Messages:
    179
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    68
    #5
    querying all links from a certain domain:

    say the domain is "domain.com"

    and there is a link like

    <a href="http://www.domain.com" target="_blank" title=""><img src="http://www.domain.com/images/domaiimage.jpg" alt="" /></a>

    on the page.. I want all anchors returns including that domain.com in the href. All links basically with "domain.com" in the href.
     
    Shoe ButtBack, Mar 18, 2010 IP
  6. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #6
    you can use
    
    /href="(.*?)"/
    
    Code (markup):
    to get link in the href
     
    guardian999, Mar 18, 2010 IP
  7. Shoe ButtBack

    Shoe ButtBack Active Member

    Messages:
    179
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    68
    #7
    What exactly does the "?" do here? I understand .* means anything but carriage return, but whats the ? for?

    Armin
     
    Shoe ButtBack, Mar 20, 2010 IP
  8. Latox

    Latox Active Member

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    96
    #8
    w ww.regular-expressions.info/php.html
     
    Latox, Mar 20, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    danx10, Mar 20, 2010 IP
  10. Shoe ButtBack

    Shoe ButtBack Active Member

    Messages:
    179
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    68
    #10
    Figured it out.

    Now .. to the next problem :)

    I now need to do the opposite. I need to grab urls that are NOT of a certain domain...

    how can I negate a sequence of characters

    if I did something like [^domain.com] , it wouldnt work since it negates each character individually no?
     
    Last edited: Mar 20, 2010
    Shoe ButtBack, Mar 20, 2010 IP