views:

73

answers:

3

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

+2  A: 

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!

Tim Fountain
A: 

To do so you must use regular expressions as you may know.

I found this pattern that could be useful for your project:

<?php
  preg_match("/(^(([\+]\d{1,3})?[ \.-]?[\(]?\d{3}[\)]?)?[ \.-]?\d{3}[ \.-]?\d{4}$)/", $yourText, $matches);
  //matches variable will contain the array of matched strings
?>   

More information about this pattern can be found here http://gskinner.com/RegExr/?2rirv where you can even test it online. It's a great tool to test regular expressions.

João Gala Louros
A: 

preg_match($pattern, $subject) will return 1 (true) if pattern is found in subject, and 0 (false) otherwise.

A pattern to match the example you give might be '/\d{3}-\d{3}\d{4}/'

However whatever you choose for your pattern will suffer from both false positives and false negatives.

You might also consider looking for words like mob, cell or tel next to the number.

The fill details of the php pattern matching can be found at http://www.php.net/manual/en/reference.pcre.pattern.syntax.php

Ian

p.s. It can't be done for bad words, as the people in Scunthorpe will tell you.

Ian