Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect telephone numbers in a text (and replace them)?

Tags:

string

php

I know it can be done for bad words (checking an array of preset words) but how to detect telephone numbers in a long text? I'm building a website in PHP for a client who needs to avoid people using the description field to put their mobile phone numbers..(see craigslist etc..)

beside he's going to need some moderation but i was wondering if there is a way to block at least the obvious like nnn-nnn-nnnn, not asking to block other weird way of writing like HeiGHT*/four*/nine etc...

like image 280
Francesco Avatar asked Sep 21 '10 21:09

Francesco


People also ask

What is the format of a phone number?

North American phone numbers To format phone numbers in the US, Canada, and other NANP (North American Numbering Plan) countries, enclose the area code in parentheses followed by a nonbreaking space, and then hyphenate the three-digit exchange code with the four-digit number.

How is a phone number Split?

In the U.S. and Canada, the parts of a phone number are the exit code, country code, area code, telephone prefix, and line numbers. The exit code lets you dial out of your home country. The country code is an identifier for a specific country. The area code directs calls to a broad region in the U.S. and Canada.

Why do they put words in phone numbers?

Why did telephone numbers start with a word? Because it made it a little easier to remember the number. Instead of 7 digits, you had an exchange name followed by a digit, plus four digits representing the local number.

What is an example of a valid phone number?

1-212-456-7890.


1 Answers

Welcome to the world of regular expressions. You're basically going to want to use preg_replace to look for (some pattern) and replace with a string.

Here's something to start you off:

$text = preg_replace('/\+?[0-9][0-9()\-\s+]{4,20}[0-9]/', '[blocked]', $text);

this looks for:

a plus symbol (optional), followed by a number, followed by between 4-20 numbers, brackets, dashes or spaces, followed by a number

and replaces with the string [blocked].

This catches all the obvious combinations I can think of:

012345 123123
+44 1234 123123
+44(0)123 123123
0123456789
Placename 123456 (although this one will leave 'Placename')

however it will also strip out any succession of 6+ numbers, which might not be desirable!

like image 162
Tim Fountain Avatar answered Sep 21 '22 15:09

Tim Fountain