Remove Email Addresses From All Posts

Discussion in 'PHP' started by godrob, Mar 4, 2008.

  1. #1
    Hi guys, I'm new to PHP so please be gentle

    I have a PHP Chat Room Script that works really well. However, what I need to be able to do is stop/prevent users from posting their email addresses.

    I've been using the following code to mask the @ with ***** However this isn't really good enough, and I need a way to replace the entire email address.

    $message = ereg_replace ("@", "*****", $message);

    Can I use something like the above to mask all email addresses from posts?

    Thanks in advance for any help
    Regards
    Rob
     
    godrob, Mar 4, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Maybe something like this:

    
    
    $emailReplace = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
    
    $cleanMessage = preg_replace($emailReplace,'*****',$message);
    
    
    PHP:
    You may need to play with it a bit, but that should remove an email address from a string.
     
    jestep, Mar 4, 2008 IP
  3. godrob

    godrob Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Jestep, this works great if only the email address is posted. However, it fails if the email address is part of a sentence.

    For example If a user was to post something like:

    "My email address is rob@robtest.com"

    Then the email address is still displayed

    Any thoughts?

    Thanks for your time
    Rob.
     
    godrob, Mar 4, 2008 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    Here is the official standard RFC 2822 regex for email:

    
    (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
    
    Code (markup):
    :D

    Or maybe a simpler one will help:

    
    \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
    
    Code (markup):
    If you want to implement it with php, something like this should work:

    
    //.*([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}).*
    //or using the above posted expression:
    $emailReplace = "/.*[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}.*/i";
    //or
    $emailReplace = "/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i";
    
    PHP:
    Peace,
     
    Barti1987, Mar 4, 2008 IP
  5. godrob

    godrob Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That worked a treat. Thank you very much

    Rob
     
    godrob, Mar 4, 2008 IP