whats wrong with this regex

Discussion in 'PHP' started by ajnin, Sep 2, 2005.

  1. #1
    eregi("<b class=author>(.*?)</b>", $content, $author)
    PHP:
    returns Warning: eregi(): REG_BADRPT in

    i am trying to get the result for the first closing tag for </b> if i do
    eregi("<b class=author>(.*)</b>", $content, $author)
    PHP:
    i get all the stuff until the final </b> closing tag.
     
    ajnin, Sep 2, 2005 IP
  2. domokun

    domokun Peon

    Messages:
    151
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you need to escape the /
     
    domokun, Sep 2, 2005 IP
  3. ajnin

    ajnin Well-Known Member

    Messages:
    407
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    108
    #3
    i tried that and it still returns the error, thanks though
     
    ajnin, Sep 2, 2005 IP
  4. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Did you try escaping the "<" ? I had some problems with regexing the "<" character using the MT regex plugin yesterday. Sorry... just shooting from the hip. It looks like it should be valid on the surface to me.
     
    nevetS, Sep 2, 2005 IP
  5. Stevo

    Stevo Peon

    Messages:
    9
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You don't need the question mark. Try this instead:

    
    $content = "read this great book by <b class=author>robert crais</b> at my website";
    eregi('<b class=author>(.*)</b>', $content, $author);
    print_r($author);
    
    PHP:
    Also, unless you have a reason for using the ereg functions for regular expression matching, try using the preg ones instead. They are a lot more powerful and faster in most cases.
     
    Stevo, Sep 5, 2005 IP
  6. ajnin

    ajnin Well-Known Member

    Messages:
    407
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    108
    #6
    sure that seems to work fine if there are no other instance of the closing </b>

    for example :
       $content = "read this great book by <b class=author>robert crais</b> at my website <b>this is where it errors</b>";
          eregi('<b class=author>(.*)</b>', $content, $author);
          print_r($author); 
    PHP:
    returns : Array ( [0] => robert crais at my websitethis is where it errors [1] => robert crais at my websitethis is where it errors )
     
    ajnin, Sep 6, 2005 IP
  7. Stevo

    Stevo Peon

    Messages:
    9
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That's happening because the regular expression is being too greedy. When this happens you need to be more precise with the regular expression and specify the characters that you expect to see between your parenthesis.

    Try this instead:
    
    $content = "read this great book by <b class=author>robert crais</b> at my website <b>this is where it errors</b>";
    eregi("<b class=author>([A-Za-z ']+)</b>", $content, $author);
    print_r($author);
    
    PHP:
    If you expect to have other characters except the A-Z, a-z, space and apostrophe then you will need to add them to the list within the square brackets. Also note I've changed the single quotes in the first parameter of the eregi call to double quotes.
     
    Stevo, Sep 6, 2005 IP
  8. ajnin

    ajnin Well-Known Member

    Messages:
    407
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    108
    #8
    awesome, thank you very much, i have been looking and searching for a anwser for days.
     
    ajnin, Sep 6, 2005 IP
  9. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #9
    If you use preg_match rather than ereg then you can control the greediness of the regular expression, and so you won't have to specify every conceivable character which could fit between the tags.
     
    johnt, Sep 7, 2005 IP
  10. ajnin

    ajnin Well-Known Member

    Messages:
    407
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    108
    #10
    johnt, I learn by example, could you by chance show me a example from the above code samples?
     
    ajnin, Sep 8, 2005 IP
  11. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #11
    This simple modification still uses the eregi function, but will match any character up to a '<', meaning you won't have to explicitly specify all the characters you wish to match:

    
    $content = "read this great book by <b class=author>robert crais</b> at my website <b>this is where it errors</b>";
    eregi("<b class=author>([^<]+)</b>", $content, $author);
    print_r($author); 
    
    PHP:
     
    Willy, Sep 8, 2005 IP
  12. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I think
    $author = preg_match("/<b class=author>(.*)<\/b>/U", $content);
    Code (markup):
    should do it but I can't test at the mo

    cheers

    John
     
    johnt, Sep 8, 2005 IP