简体   繁体   English

正则表达式以匹配数字中的数字单词

[英]regex to match digit word in numbers

Here is my regex to validate the phone number. 这是我用来验证电话号码的正则表达式。 which works fine for various numbers like 1223534345 etc . 它适用于各种数字,如1223534345等。

/(?:\((\+?\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. 1223five34345 ,也应将其视为1223534345并应进行过滤。 Is this possible ? 这可能吗 ?

Explanation : 说明:

Map all those words as shown in the $arrwords , now do a preg_match_all() to check for all the occurrences of the words. 映射所有这些单词,如$arrwords所示,现在执行preg_match_all()来检查单词的所有出现情况。 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. 现在循环该数组,并检查$arrwords数组中是否存在该值(如果找到),将键映射到字符串。

<?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. 现在,您可以使用正则表达式验证此$str

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM