Reputation: 2257
Here is my regex to validate the phone number. which works fine for various numbers like 1223534345
etc .
/(?:\((\+?\d+)?\)|(\+\d{0,3}))? ?\d{2,3}([-\.]?\d{2,3} ?){3,4}/
snippet:
foreach ($words as $word){
$arrwords = array(0=>'zero',1=>'one',2=>'two',3=>'three',4=>'four',5=>'five',6=>'six',7=>'seven',8=>'eight',9=>'nine');
preg_match_all('/[A-za-z]+/', $text, $matches);
$arr=$matches[0];
foreach($arr as $v)
{
$text= str_replace($v,array_search($v,$arrwords),$text);
}
$pattern = '/(?:\((\+?\d+)?\)|(\+\d{0,3}))? ?\d{2,3}([-\.]?\d{2,3} ?){3,4}/';
preg_match_all($pattern, $text, $matches, PREG_OFFSET_CAPTURE );
$this->pushToResultSet($matches);
}
But I want to add exploits like this:
1223five34345
, it should also be considered as 1223534345
and should be filtered. Is this possible ?
Upvotes: 1
Views: 453
Reputation: 68476
Map all those words as shown in the $arrwords
, now do a preg_match_all()
to check for all the occurrences of the words. Grab them in array. Now loop that array and check if that value exists in the $arrwords
array , if found map the key to the string.
<?php
$arrwords = array(0=>'zero',1=>'one',2=>'two',3=>'three',4=>'four',5=>'five',6=>'six',7=>'seven',8=>'eight',9=>'nine');
$str='I won total five prize';
preg_match_all('/[A-za-z]+/', $str, $matches);
$arr=$matches[0];
foreach($arr as $v)
{
$v = strtolower($v); //<--- Fail safe !!
if(in_array($v,$arrwords))
{
$str = str_replace($v,array_search($v,$arrwords),$str);
}
}
echo $str; //I won total 5 prize
You can now validate this $str
with your regular expression.
Upvotes: 2