Need help with a REGEXP

Discussion in 'PHP' started by phantom, Mar 28, 2011.

  1. #1
    I am trying to match

    some bbcode in the form of

    [quota=234]

    But can't seem to get it to work

    what I have so far is:
    if(preg_match("/\[quota=[0-9]\]/i",$string)){
    //do stuff
    }
    Code (markup):
    but it is not working.....somehow I need to escape the [ brackets.

    An Idea s to how I can accomplish this?
     
    phantom, Mar 28, 2011 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    
    <?php
    
    $string = "[quota=123456]This is our test string[/quota]";
    
    if(preg_match("/\[quota=(.*)\]/i",$string)){
    echo "Match Found";
    }else{
    echo "No Match";
    }
    
    ?>
    
    PHP:
     
    MyVodaFone, Mar 28, 2011 IP
  3. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #3
    thanks MyVodaPhone ...it works perfectly.

    Can you tell me why the change to (.*) makes it work?

    When I do this preg_match("/quota=[0-9]/i",$string) it works for matching quota=123

    But why not when I try [quota123 and the outer brakets....but your works with the change to (.*).

    Glad your version works just curious why mine didn't so I can learn:)


    Thanks for your help!
     
    phantom, Mar 28, 2011 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    Well your initial search was including the [brackets] so I figured as long as we had an [opening and closing] bracket it didn't mater what quota was equalled to. If that's a problem let us know....
     
    MyVodaFone, Mar 28, 2011 IP
  5. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #5
    I guess I need to read up on REGEXP more because I still don't get it:)
    Anyway thanks for your help.... your solution is working excellent for what I need.

    Thanks again.
     
    phantom, Mar 28, 2011 IP
  6. tianli

    tianli Peon

    Messages:
    116
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    it looks good
     
    tianli, Mar 28, 2011 IP