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
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.
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.
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): 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,