Regex Match in PHP

Discussion in 'PHP' started by NaSh123, Sep 2, 2011.

  1. #1
    Hey Guys,

    I'm looking to match the following string but having a bunch of problems!

    String I want to match:
    test@test.com
    Code (markup):
    String I am trying to pull it out of:
    003cdiv class=\"data_field\">test@test.com\u003c\/div>
    Code (markup):
    Current REGEX I'm using:
    preg_match_all('/^[^@]*@[@]*\.[^@]*$/', $this->markup, $email);
    Code (markup):
    Thanks!
     
    NaSh123, Sep 2, 2011 IP
  2. iBank ™

    iBank ™ Peon

    Messages:
    63
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    0
    #2
    preg_match_all("/[a-zA-Z]+[@]+[a-zA-Z]+.[a-zA-Z]+/i", $string, $matched);
    PHP:
    test@test.com
    Code (markup):
     
    iBank ™, Sep 2, 2011 IP
  3. NaSh123

    NaSh123 Peon

    Messages:
    1,298
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks but I'm looking to match those characters instead of the actual @ sign. Should I just replace the @ sign with those characters?
     
    NaSh123, Sep 2, 2011 IP
  4. iBank ™

    iBank ™ Peon

    Messages:
    63
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    0
    #4
    You can use character code if you wish.
     
    iBank ™, Sep 3, 2011 IP
  5. HalvinCarris

    HalvinCarris Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That's right.
     
    HalvinCarris, Sep 3, 2011 IP
  6. NaSh123

    NaSh123 Peon

    Messages:
    1,298
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Not sure what you mean by that...exact characters?
     
    NaSh123, Sep 5, 2011 IP
  7. gvre

    gvre Member

    Messages:
    35
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    33
    #7
    Try this
    $data = "003cdiv class=\"data_field\">test@test.com\u003c\/div>";
    $pattern = '#([a-z\-\.\_]+)&\#64;([a-z\-\.]+)#si';
    if (preg_match($pattern, $data, $m))
    {
             $email = $m[1] . "@" . $m[2];
             echo $email;
    }
     
    PHP:
     
    gvre, Sep 7, 2011 IP
  8. NaSh123

    NaSh123 Peon

    Messages:
    1,298
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks gvre that seems to have worked! Could you explain the array 1 and 2? Is it storing it in 2 different areas, thats why?
     
    NaSh123, Sep 15, 2011 IP
  9. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #9
    The array is the value returned from the preg_match function. Have a look at when he calls the function he declares $m at the very end, then in turn that allows the programmer to be able to collect those array values. The 1 represents the first condition that matched, the 2 is the second.

    More info here: http://php.net/manual/en/function.preg-match.php

    Kind regards,

    Glen
     
    HuggyEssex, Sep 16, 2011 IP