2

I was hoping for a little help on this, as it's confusing me a little... I run a website that allows users to send messages back and forth, but on the inbox i need to hide both emails and phone numbers.

Example: This is how a sample email would look like.

Hi, my phone is +44 5555555 and email is jack@jack.com

I need it to be like this:

Hi, my phone is (phone hidden) and email is (email hidden)

Do you have any ideas ?... I really appreciate it!..

Jack Johnson
  • 593
  • 4
  • 11
  • 23

5 Answers5

5
$x = 'Hi, my phone is +44 5555555 and email is jack@jack.com';
$x = preg_replace('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i','(phone hidden)',$x); // extract email
$x = preg_replace('/(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?/','(email hidden)',$x); // extract phonenumber
echo $x; // Hi, my phone is (phone hidden) and email is (email hidden)

kudo's for the phonenumber regex to fatcat

Community
  • 1
  • 1
1

Trying to do this with 100% accuracy when users can type all sorts of things in is impossible - you can't really definitively say if a substring is a phone number or just another number, or an email address or just something that could be a valid one.

However, if you want to try, you should probably use a regular expression. See http://php.net/manual/en/function.preg-replace.php

wizzardmr42
  • 1,634
  • 12
  • 22
  • 1
    Be wary of using regular expressions to match emails; you can easily do it wrong. See this answer for details: http://stackoverflow.com/a/201378/643309 – WilliamMayor Jul 13 '12 at 11:57
1
<pre>
/*
* first par is given string
* second per is replace string like ****
* return result 
*/
function email_phone_validation_replace_php($str='',$rep='*******') { 

    $str = preg_replace('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i',$rep,$str); // extract email
    $str = preg_replace('/(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?/',$rep,$str); // extract phonenumber
    return  $str; // banarsiamin@gmail.com
}
$str = 'Hi, my name is amin khan banarsi <br> phone is +91 9770534045 and email is banarsiamin@gmail.com';
 echo email_phone_validation_replace_php($str);
</pre>
amin ntier
  • 11
  • 1
0

If I understand correctly, your users can send messages to each other and you're worried that if they send a message with personal information in it that information might be too visible.

I guess that you're therefore trying to remove this information from the message's preview (but still have it available if you open the message?).

If this is the case then you can have a really sloppy regular expression removing anything that looks even a little bit like a number or email. It doesn't matter if you hide non-personal information because the non-censored version of the message is always available.

I would go with something like this (untested):

# Take any string that contains an @ symbol and replace it with ...
# The @ symbol must be surrounded by at least one character on both sides
$message = preg_replace('/[^ ]+@[^ ]+/','...',$message); # for emails
# Take any string that contains only numbers, spaces and dashes, replace with ...
# Can optionally have a + before it.
$message = preg_replace('/\+?[0-9\- ]+/','...',$message); # for phone numbers

This is going to match lots of things, more than just emails and phone numbers. It may also not match emails and phone numbers that I didn't think of, this is one of the problems with writing regular expressions for these kinds of things.

WilliamMayor
  • 745
  • 6
  • 15
0

If you want to hide email and phone numbers from your Messages or Chat in PHP or any other language. You need to use regular expressions, read about regex on w3school.

I have an easy and complete solution for you.

<?php       
    $regex_email = '/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/';
    $regex_phone = "/[0-9]{5,}|\d[ 0-9 ]{1,}\d|\sone|\stwo|\sthree|\sfour|\sfive|\ssix|\sseven|\seight|\snine|\sten/i";

    
    $str = " Hello My Email soroutlove1996@gmail.com AND Phone No. is +919992799999 and +91 9992799999, or 9 9 9 2 7 9 9 9 9 9 and Nine Nine";;
    $str = preg_replace($regex_email,'(email hidden)',$str); // extract email
    $str = preg_replace($regex_phone,'(phone hidden)',$str); // extract phone
    
    echo $str;

Output: Hello My Email (email hidden) AND Phone No. is +(phone hidden) and +(phone hidden), or (phone hidden) and(phone hidden)(phone hidden)

Surya Pratap
  • 59
  • 2
  • 6