Jump to content

Filtering out phone numbers from a string...


nevynev

Recommended Posts

Hi,

 

Im making a profanity, email and phone number filter script. So far I've tackled swearing and emails but phone numbers is a little more tricky.

 

How would I scan for a group of numbers more than 6 characters long in any format e.g.

 

012345678

01234 567 891

0 1 2 3 4 5 6 7 8 9 1

0-1-2-34-5-6789

0*1#2+3456]4

 

And so on... I know its regular expressions but I want to have it so it picks up all kinds of combos but doesnt mess up e.g. someone typing in e.g. £100.00 (less than 6 consecutive characters). Of course if someone really wanted to work around it they could but I want to put people off trying.

 

Thanks for any help!

NevyNev

Link to comment
Share on other sites

I'm not up on the regex terminology or syntax, but I think it sounds like you want something like ...

(and I don't know how many characters you want them to be able to check for in between the numbers)

 

Any number, 0-2 other characters, Any number, 0-2 other characters, etc.. until you get to however long the phone number is.

"^([0-9](.{0-2}){10}$"

 

I have no idea if that will work.

 

But that wouldn't like people type in something like... $10,000,000.00 because it satisfies that condition it seems, though I haven't tested it.

Link to comment
Share on other sites

Hmm that's a tricky one...

 

how about starting simply:

$string ='My telephone number is: 01111 111 111';

 

Then cut out all the spaces:

 

$check=explode(' ', $string);

$new_string=implode($check);

You can then do the same with all the other characters.

Once you have done that you either do a counter script for each time you come across a number or cut all of them out:

$num=array('0','1','2','3','4','5','6','7','8','9');

$no_of_numbers=explode($num, $new_string);

 

To do a count script use a for_each loop with a ++ incrementing variable.

 

Hope this helps, but doubt it does because I seem to have confused myself.

 

 

 

Link to comment
Share on other sites

Please do not double post. If you want your topic in the Regex forum I'll move it there. Pick one.

 

This is a dangerous task since you're allowing any format. Can you narrow it down a bit?

 

<pre>
<?php
$data = <<<DATA
012345678
01234 567 891
0 1 2 3 4 5 6 7 8 9 1
0-1-2-34-5-6789
0*1#2+3456]4
$10,000,000.00
The answer is 6.0221415 E+23.
Meet me @ 5pm at Apartment 1 on 1500 Elm Street.
DATA;
echo preg_replace('#
	(?!\$)
	(?:\d[^\d,.\r\n]*){6,}
#x', '', $data);
?>
</pre>

 

Yields:

$10,000,000.00

The answer is 6..

Meet me @ .

Link to comment
Share on other sites

Well my main aim is to deter people from entering their phone numbers within any text field on the site, but it doesn't have to completely block all just most normaly attempts I suppose.Hope that narrows it down a bit.

 

How about a search from when it finds a character which is a number and then checks the next e.g. 16 characters and if say 8 out of those 16 characters are numbers then it returns an error, assuming the numbers make up a phone number or someone trying to work around it....

 

Make sense?

Thanks

nevyNev

Link to comment
Share on other sites

It is a form of social networking site so I guess people will be entering all kinds of things but not really anything scientific etc.

 

From the other post - His solution:

 

true must admit i didn't think of that (must be having an off day here) however i have now written the code to remove phone numbers but leave in in numbers such as 1000.00 or 1000. Im sure its not perfect but here it is

 

"/([\(\+])?([0-9]{1,3}([\s])?)?([\+|\(|\-|\)|\s])?([0-9]{3,4})([\-|\)|\.|\s]([\s])?)?([0-9]{2,4})?([\.|\-|\s])?([0-9]{4,8})/","/\b()\b/i"

 

Would like to know what you all think

 

Is that too over the top? I.e. will I lose numbers like 100,000 or £1000000. I don't want to irritate my members.

 

What do you think of his solution?

 

Thanks

NevyNev

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.