Regex to check if string contains more than...

Discussion in 'PHP' started by x0x, Aug 1, 2009.

  1. #1
    Can anyone help me with a regex to check if a string contains more than

    . (dot)
    a-z (both cases)
    1-9
    @ (at)

    I'm trying to find the special characters etc.
     
    x0x, Aug 1, 2009 IP
  2. Ultimate_coder

    Ultimate_coder Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    \w+ ,[a-z]+ is helpful
     
    Ultimate_coder, Aug 1, 2009 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Forgot, I want to pull every email from a database that doesn't contain a .,@,_,-,a-z,1-9

    \w+ ,[a-z]+ didn't give me the results I expected..
     
    x0x, Aug 1, 2009 IP
  4. XDMCoder

    XDMCoder Peon

    Messages:
    50
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $con = mysql_connect("localhost","qwerty","qwerty");
    mysql_select_db("yourDB", $con);
    $result = mysql_query("SELECT * FROM users");
    
    while($row = mysql_fetch_array($result))
    {
    	if(!preg_match("/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+\.[a-zA-Z0-9]{2,4}$/", $row['email']))
    	{
    		$id=$row['id'];
    		mysql_query("DELETE FROM users WHERE id='$id'");
    	}
    }
    
    mysql_close($con);
    PHP:
     
    XDMCoder, Aug 1, 2009 IP
  5. XDMCoder

    XDMCoder Peon

    Messages:
    50
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You're welcome :)
     
    XDMCoder, Aug 1, 2009 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    You don't actually have to pull anything from a MySQL database if all you're going to do is delete what is found. MySQL supports POSIX compliant (extended) regular expressions via REGEXP.

    You can accomplish the same thing with a query similar to this.

    DELETE FROM list
    WHERE email NOT REGEXP '^[a-z0-9_.-]+@[a-z0-9_.-]+\.[a-z0-9]{2,4}$'
    Code (markup):
     
    joebert, Aug 2, 2009 IP
  7. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Learning new things everyday! Thanks.
     
    x0x, Aug 6, 2009 IP