簡體   English   中英

如果javascript被禁用,你如何過濾(和替換)手機#輸入你想要(XXX)XXX-XXXX完全,在PHP或正則表達式

[英]How do you filter (and replace) phone# input if javascript disabled and you want (XXX)XXX-XXXX exactly, in PHP or Regex

我有一個jQuery使用掩碼很好地處理這個問題,問題是用戶沒有javascript。 這種情況很少見,但我們希望有所退步。 現在我們的正則表達式回退是模塊中的內置工具,確認了10個數字,就是這樣。 我想輸入XXX-XXX-XXXX ,輸入XXXXXXXXXX或其他任何轉換為(XXX)XXX-XXXX 我願意用PHP編寫代碼來處理這個問題,但是如果有一個正則表達式或者某個已經標准化的東西,我還沒有看到我會感興趣。這個想法是找到一些完全證明的東西,所以這可以傳遞給一個數據庫。 此表單的一個示例位於http://www.gestationaldiabetic.com

我不是一個PHP人,但最簡單的方法是刪除除數字之外的所有內容,然后在傳入數據庫之前重新創建字符串。

只要你得到10位數,讓用戶輸入它們。

這是一個虛擬的。

function format_phone($num) {
    $num = preg_replace('/[^\d]/','',$num);
    if (strlen($num) != 10) return 0;
    // this requires the number to contain 10 digits
    return preg_replace('/^(\d{3})(\d{3})(\d{4})$/','($1)$2-$3',$num);
}

$res = format_phone('0123456789');
if (!$res) {
    echo 'Phone number should contain a 3-digit area code and a 7-digit local phone number.';
} else {
    echo $res;
    // INSERT the number in your DB
}

Welp,我使用以下正則表達式:

^[^2-9]*([2-9])\D*(\d)\D*(\d)[^2-9]*([2-9])(?:\D*(\d)[^02-9]*([02-9])|[^02-9]*([02-9])\D*(\d))\D*(\d)\D*(\d)\D*(\d)\D*(\d).*$

然后使用替換模式:

$1$2$3-$4$5$6$7$8-$9$10$11$12

它允許任何這些按照指示重新格式化:

Input                                                                Output
---------------------                                                --------
2345678905                                                           234-567-8905
2345678905                                                           234-567-8905
234-569-8956                                                         234-569-8956
223.5876954                                                          223-587-6954
4444444444                                                           444-444-4444
 2359996574                                                          235-999-6574
 777-873 6542                                                        777-873-6542
      (800) 874 8321                                                 800-874-8321
223...587...9999                                                     223-587-9999
1-800-345-6734                                                       800-345-6734
8.5.3-2.9.7-6.5.4.3                                                  853-297-6543
+1 (976) 566-7763                                                    976-566-7763
   0154388640         .           -              33                  543-886-4033
%4*555@123$5^78                                                      455-512-3578
#445#2984$32@9                                                       445-298-4329
$123,456,987.20                                                      234-569-8720
 123,456,987.20                                                      234-569-8720
 $23,456,987.20$                                                     234-569-8720
3/9/1954 @21:33                                                      391-954-2133
12345678901234567890                                                 234-567-8901
55555555555555555555555555                                           555-555-5555
lkajshdf28jksa23:'\\\fg[]ewr]?.,,ewf3412233445566gwqerq              282-334-1223
$123,456,987.20 dollars                                              234-569-8720
$223,456,987.20                                                      223-456-9872
222-211-45674                                                        222-214-5674

重要說明:它解析MIGHT代表電話號碼的前十個符合數字,實際上不允許輸入不符合Wikipedia上NANP文章中規定的規則的電話號碼。

表達式中間的五個組(組4-8)是“交換代碼”的第一個數字(#4),然后是最后兩個數字(分別為#5 /#7和#6 /#8) )交換代碼,如果它們不是1

基本上,任何真實有效的電話號碼都應該被正確解析和重新格式化。

其他一些選擇是:

This allows getting and reformatting an extension (indicated by `x`, `ext.`, `extension`, plus a few variations of those and only if all extension digits are contiguous), if supplied:
Parsing Expression
------------------
(?i)^\D*1?\D*([2-9])\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)[^x]*?\s*(?:(?:e?(x)(?:\.|t\.?|tension)?)\D*(\d+))?.*$
Replacement
-----------
$1$2$3-$4$5$6-$7$8$9$10 $11$12


This does not enforce ALL of the NANP rules, but does most of them and is bug free, according to my tests:
Parsing Expression
------------------
^[^2-9]*([2-9])\D*(\d)\D*(\d)\D*([2-9])\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d).*$
Replacement
-----------
$1$2$3-$4$5$6-$7$8$9$10

如果你願意,我可以進一步解釋或給你另一個版本!

另外,對於“編譯”代碼的效率還有一些要說的東西(JavaScript通常不是真正編譯的,.Net和Java沒有完全編譯,許多語言只能實時解析...),但有對於緊湊的源代碼也有很多話要說 - 你決定是否要讓表達式(如正則表達式)為你完成所有的工作,或者自己創建所有的循環,檢查,解析等。

暫無
暫無

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

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