在PHP中查找和替换电子邮件和电话号码 [英] Find and replace emails and phone numbers in PHP
本文介绍了在PHP中查找和替换电子邮件和电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:这是一封电子邮件的样子。
+44 5555555,电子邮件是jack@jack.com
我需要这样做:
我的手机是(手机隐藏),电子邮件是(电子邮件隐藏)
你有什么想法吗?我真的很感激!
解决方案
$ x ='我的手机是+44 5555555,电子邮件是jack@jack.com';
$ x = preg_replace('/ [A-Z0-9 ._%+ - ] + @ [A-Z0-9 .-] + \。[AZ] {2,4} / i' '(手机隐藏)',$ x); //提取电子邮件
$ x = preg_replace('/(?:( ?: \ +?1\s *(?:[.-] \s *)?)?(?: \ (\s *([2-9] 1 [02-9] | [2-9] [2月8日] 1 | [2-9] [2月8日] [02-9])\s * \)|([2-9] 1 [02-9] | [2-9] [2月8日] 1 | [2-9] [2月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\ |分机\。?| extension)\s *(\d +))?/','(email hidden)',$ x); //提取phonenumber
echo $ x; //我的手机是(手机隐藏的)和电子邮件是(电子邮件隐藏)
kudo的phonenumber regex to fatcat
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!..
解决方案
$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
这篇关于在PHP中查找和替换电子邮件和电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文