PX : code

Consider that you place a messaging system on your site from where users of your site can communicate with each other. This would really increase hits on your site but what if users give each other their email address in the message then they wont need your messaging system, hence they wont be coming to your site. So you need to censor out any email address in the messages that users send to each other.

I came up with an efficient yet simple solution to this problem. All you have to do is to search for any '@' in the message and then censor out few characters that appear after it and before it. For example, if you type "myemailaddress@hotmail.com" then the script will display it like my*********com.

Here is the how it works

For Example we got the following message:

$message = "hello this is my email address: myaddress@mydomain.com"

1. First you search for '@' in the message that is passed to the function. strrpos returns the position of the character you specify.

$emailpos = strrpos($message,'@');
This line will return the value '42'.

2. Then we check if there was a '@' in the message if none then we simply return the passed message other wise we proceed to the next step.

3. Then we take out the sub string (email address) based on the position we got from above. For Example, if we got the following message.

$email = substr($message,$emailpos-5,12 );
Here we start the sub string by first going back 5 characters and then taking out next 12 characters. This will return 'dress@mydoma' you can change the length by changing values in the above function. I only used 12 characters because i think that is sufficient. You can change this according to your needs.

4. Then in the final step we simplly need to replace this substring (email address) with anything we like. Iprefer using '******'. You can use whatever you want.

$message =eregi_replace($email,"********",$message);

5. Thats it! Now you just return the censored $message.

Below is the full code listing. You can just copy paste this function in your code and use it.
*/