preg_match_all - Need some help

Discussion in 'PHP' started by PHPGator, Feb 28, 2008.

  1. #1
    Hello all,

    Let me just get straight to the mix.

    I have a string: {this is} a {test}.

    I want to capture both "this is" and "test" in an array using preg_match_all (or another function if something else should be applied here).

    I simply can't figure out how to do it. Any help on that would be great.
     
    PHPGator, Feb 28, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    preg_match_all('~\{([^\}]+)\}~', $string, $matches);
    
    print_r($matches[1]);
    
    PHP:
     
    nico_swd, Feb 28, 2008 IP
    PHPGator likes this.
  3. PHPGator

    PHPGator Banned

    Messages:
    4,437
    Likes Received:
    133
    Best Answers:
    0
    Trophy Points:
    260
    #3
    Thanks that worked great (rep given). Now I ran into another problem that I know i'm doing something stupid on I just can't quite figure it out.

    When I try to echo($matches[0]); it returns the term "Array" rather than actually what is inside of it. What am I doing wrong here? :D

    I have tried both print() and echo().
     
    PHPGator, Feb 28, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    It should be:
    
    echo $matches[1][0]; // this is
    echo $matches[1][1]; // test
    
    PHP:
    If you do a:
    
    print_r($matches);
    
    PHP:
    ... you'll see everything clearer.
     
    nico_swd, Feb 28, 2008 IP
  5. PHPGator

    PHPGator Banned

    Messages:
    4,437
    Likes Received:
    133
    Best Answers:
    0
    Trophy Points:
    260
    #5
    A hah! I see now. Sorry about the dumb question. ;)

    Thanks,
    -Travis T.
     
    PHPGator, Feb 28, 2008 IP
  6. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Curious, I've seen you use that many times. What's the difference between using ~ and using / as the start/end of a match expression? (I've always used / because I saw that on PHP's website)
     
    zerxer, Feb 28, 2008 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    It's the same basically. But I prefer ~, so that I don't have to escape all slashes that I want to match.

    Let's take this pattern as example. It checks if a string begins with http://

    
    '~^http://~'
    
    PHP:
    If I use the slash as delimiter, I'd have to escape all slashes:
    
    '/^http:\/\//'
    
    PHP:
    ... which is ugly. So I prefer ~. Now I'm using it always 'cause I got used to it. :p

    But other than that, it's the same... You can also use @, !, |, etc... as delimiter.
     
    nico_swd, Feb 28, 2008 IP
    zerxer likes this.
  8. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ah, I see. Most of the time I don't like using any delimiters so I just stick with ereg/eregi (I don't see any differences except not being able to put modifiers at the end of it, which the only one I ever use is 'i' and eregi already does case-insensitive).
     
    zerxer, Feb 28, 2008 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    The preg_* functions are about 6 times faster than the ereg_* functions.

    Now for small strings like URLs, it doesn't matter, but I generally prefer the preg_* functions.
     
    nico_swd, Feb 28, 2008 IP
  10. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #10
    I never realized the preg_* functions were faster than ereg_*s.
    I originally decided to use them just because they seemed more powerfull and configurable.
     
    joebert, Feb 28, 2008 IP