在PHP中查找和替换电子邮件和电话号码


Find and replace emails and phone numbers in PHP

我希望在这方面能得到一些帮助,因为这让我有点困惑。。。我经营着一个网站,允许用户来回发送信息,但在收件箱中,我需要隐藏电子邮件和电话号码。

示例:这就是示例电子邮件的样子。

嗨,我的电话是+44 5555555,电子邮件是jack@jack.com

我需要它是这样的:

嗨,我的手机(手机隐藏)和电子邮件(电子邮件隐藏)

你有什么想法吗?。。。我真的很感激!。。

$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的电话号码regex到fatcat

当用户可以键入各种内容时,试图以100%的准确性做到这一点是不可能的——你不能真正明确地说子字符串是电话号码还是另一个号码,或者电子邮件地址,或者只是可能是有效的。

但是,如果您想尝试,您可能应该使用正则表达式。看见http://php.net/manual/en/function.preg-replace.php

<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>

如果我理解正确,您的用户可以相互发送消息,并且您担心如果他们发送的消息中包含个人信息,则该信息可能过于可见。

因此,我猜您正试图从邮件的预览中删除此信息(但如果您打开邮件,它仍然可用?)。

如果是这样的话,那么你可以用一个非常草率的正则表达式来删除任何看起来有点像数字或电子邮件的东西。如果你隐藏非个人信息并不重要,因为信息的非审查版本总是可用的。

我会选择这样的东西(未经测试):

# 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

这将匹配很多东西,不仅仅是电子邮件和电话号码。它也可能与我没有想到的电子邮件和电话号码不匹配,这是为这类事情编写正则表达式的问题之一。

如果您想在PHP或任何其他语言的消息或聊天中隐藏电子邮件和电话号码。您需要使用正则表达式,请阅读w3school上的regex。

我有一个简单而完整的解决方案给你。

<?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;

输出:你好我的电子邮件(隐藏电子邮件)和电话号码是+(电话hidden)和+(隐藏的手机),或(隐藏的电话)和(隐藏的电脑)(手机隐藏)