Find and replace emails and phone numbers in PHP

瘦欲@ 提交于 2019-12-22 17:39:46

问题


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


回答1:


$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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/11469957/find-and-replace-emails-and-phone-numbers-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!