簡體   English   中英

正則表達式以匹配數字中的數字單詞

[英]regex to match digit word in numbers

這是我用來驗證電話號碼的正則表達式。 它適用於各種數字,如1223534345等。

/(?:\((\+?\d+)?\)|(\+\d{0,3}))? ?\d{2,3}([-\.]?\d{2,3} ?){3,4}/

片段:

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);
}

但是我想添加這樣的漏洞:

1223five34345 ,也應將其視為1223534345並應進行過濾。 這可能嗎 ?

說明:

映射所有這些單詞,如$arrwords所示,現在執行preg_match_all()來檢查單詞的所有出現情況。 抓住他們的數組。 現在循環該數組,並檢查$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

現在,您可以使用正則表達式驗證此$str

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM