lua实现美国电话号码生成函数

某些情况下需要随机美国电话号,根据不同的州生成各各州的电话号,注意这里的随机号码只是确保电话号码与州能匹配,但不一定证明号码一定存在.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
local phestate={} --这个表数据,保存哪些州的号码开头几位数.
phestate[ "CA" ] = { "209" , "310" , "323" , "408" , "415" , "510" , "530" , "559" , "562" , "619" , "626" , "650" , "661" , "707" , "714" , "760" , "805" , "818" , "831" , "858" , "909" , "916" , "925" , "949" ,}
phestate[ "HI" ] = { "808" ,}
phestate[ "DE" ] = { "302" ,}
phestate[ "MI" ] = { "231" , "248" , "269" , "313" , "517" , "586" , "616" , "734" , "810" , "906" , "947" , "989" ,}
phestate[ "DC" ] = { "202" ,}
phestate[ "WY" ] = { "307" ,}
phestate[ "NE" ] = { "308" , "402" ,}
phestate[ "ME" ] = { "207" ,}
phestate[ "KY" ] = { "270" , "502" , "606" , "859" ,}
phestate[ "CO" ] = { "303" , "719" , "720" , "970" ,}
phestate[ "MA" ] = { "339" , "351" , "413" , "508" , "617" , "774" , "781" , "857" , "978" ,}
phestate[ "NC" ] = { "252" , "704" , "828" , "910" , "919" , "980" ,}
phestate[ "WI" ] = { "262" , "414" , "608" , "715" , "920" ,}
phestate[ "PA" ] = { "215" , "267" , "412" , "484" , "570" , "610" , "717" , "814" , "878" ,}
phestate[ "WA" ] = { "206" , "253" , "360" , "425" , "509" ,}
phestate[ "VA" ] = { "276" , "304" , "434" , "540" , "571" , "703" , "757" , "804" ,}
phestate[ "OK" ] = { "405" , "580" , "918" ,}
phestate[ "MO" ] = { "314" , "417" , "573" , "636" , "660" ,}
phestate[ "KS" ] = { "316" , "620" , "785" , "816" , "913" ,}
phestate[ "TX" ] = { "210" , "214" , "254" , "281" , "325" , "361" , "409" , "430" , "432" , "469" , "512" , "682" , "713" , "806" , "817" , "830" , "832" , "903" , "915" , "936" , "940" , "956" , "972" , "979" ,}
phestate[ "IL" ] = { "217" , "309" , "618" , "630" , "708" , "815" , "847" ,}
phestate[ "OR" ] = { "503" , "541" , "971" ,}
phestate[ "MT" ] = { "406" ,}
phestate[ "AK" ] = { "907" ,}
phestate[ "FL" ] = { "239" , "305" , "352" , "386" , "407" , "561" , "727" , "754" , "772" , "786" , "813" , "850" , "863" , "904" , "941" , "954" ,}
phestate[ "MD" ] = { "240" , "301" , "410" , "443" ,}
phestate[ "ND" ] = { "701" ,}
phestate[ "AL" ] = { "205" , "251" , "256" , "334" ,}
phestate[ "UT" ] = { "385" , "435" , "801" ,}
phestate[ "ID" ] = { "208" ,}
phestate[ "VT" ] = { "802" ,}
phestate[ "NV" ] = { "702" , "775" ,}
phestate[ "NY" ] = { "315" , "347" , "518" , "585" , "607" , "646" , "716" , "718" , "845" , "914" ,}
phestate[ "NM" ] = { "505" ,}
phestate[ "LA" ] = { "225" , "318" , "337" , "504" , "985" ,}
phestate[ "SD" ] = { "803" , "843" , "864" ,}
phestate[ "TN" ] = { "423" , "615" , "731" , "865" , "901" , "931" ,}
phestate[ "SC" ] = { "401" ,}
phestate[ "AR" ] = { "479" , "501" , "870" ,}
phestate[ "IN" ] = { "219" , "260" , "317" , "574" , "724" , "765" , "812" ,}
phestate[ "IA" ] = { "319" , "515" , "563" , "641" , "712" ,}
phestate[ "NJ" ] = { "201" , "551" , "609" , "732" , "848" , "856" , "862" , "908" , "973" ,}
phestate[ "GA" ] = { "229" , "404" , "470" , "478" , "678" , "706" , "770" , "912" ,}
phestate[ "NH" ] = { "603" ,}
phestate[ "OH" ] = { "216" , "234" , "330" , "419" , "440" , "513" , "567" , "614" , "740" , "937" ,}
phestate[ "MN" ] = { "218" , "320" , "507" , "612" , "651" , "763" , "952" ,}
phestate[ "MS" ] = { "228" , "601" , "662" ,}
phestate[ "CT" ] = { "203" , "860" ,}
phestate[ "AZ" ] = { "480" , "520" , "623" , "928" ,}
 
local function rndstr(num,bnum)
     local str = 'abcdefghijklmnhopqrstuvwxyz'
     if ( bnum ) then str = '0123456789' end
 
     local ret = ''
     for i=1 ,num do
         local rchr = math.random(1,string.len(str))
         ret = ret .. string. sub (str, rchr, rchr)
     end
 
     return ret
end
 
--这里传递州的简写,返回一个电话号码
function createPhone(state)
     if ( nil ~= phestate[state] ) then
         return phestate[state][ math.random(1, #phestate[state]) ] ..
          rndstr(3,true) .. rndstr(4,true)
     else
      local rnd = { "GA" , "NJ" , "OH" , "MN" }
         rnd = rnd[ math.random(1, #rnd)]
             return phestate[rnd][ math.random(1, #phestate[rnd]) ] ..
              rndstr(3,true) .. rndstr(4,true)
 
  end
end
文本为 LuaIE原创文章 lua实现美国电话号码生成函数

本文链接地址: http://www.luaie.com/lua-america-phone-create-function.html

你可能感兴趣的:(lua实现美国电话号码生成函数)