...

View Full Version : Regex Question for the advanced users



kevinkhan
01-06-2010, 02:01 PM
I have a regular expression to match numbers in this format

087xxxxxxx
086xxxxxxx
085xxxxxxx
083xxxxxxx
(087) xxxxxxx
(087)xxxxxxx
( 087 )xxxxxxx
( 087 ) xxxxxxx
087 xxxxxxx
087-xxxxxxx
087 - xxxxxxx


#([( ]{0,2}08[3567])[) -]{0,3}([0-9]{7})#

How do i write a regex to take numbers in the above formats but not numbers that have a digit repeated more than 4 times like this
0879811111

Thanks

wldrumstcs
01-06-2010, 10:43 PM
I'm not sure if you can do that with regexes, but here's what I would do:


// run your regex code here
// if your string passes the regex, then go on to the code below
$data = "0879811111"; // your string
$limit = 4;
foreach (count_chars($data, 1) as $i => $val)
{
if($val > $limit)
{
echo "Too many instances of \"".chr($i)."\"";
break;
}
}

JAY6390
01-07-2010, 02:02 PM
forget the regex to match, instead use it to clean it up

$data = "(087) 9811111";
$data = preg_replace('/[^\d]+/', '', $data);
if(strlen($data) != 10) {
echo 'Not enough digits';
}else{
$chars = str_split($data);
if(array_pop(array_keys(array_count_values(array_count_values($chars)))) > 3) {
echo 'Too many repetitive numbers';
}else{
echo $data;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum