ハイフン無しの電話番号に、適切なハイフンを入れるWebサービスを作ってみた。
コンピュータは、文字列を結合するのは得意だけど、不規則に分けるのは苦手!
郵便番号みたいに3ケタ+4ケタ=7ケタと決まりきっていれば簡単なのですが、市外局番みたいなのは不規則なのでわりと大変(^_^;)
サンプル顧客データでも、ハイフンなしだったので自分で作ってみました。
市外局番の情報は、総務省のHPからダウンロードできるみたいなので、先頭一致した市外局番から計算してみた。
試してみたい人は、以下のページから試してみて下さい。
http://fddcddhdd.ready.jp/area_code.php
ソースは以下の通り。
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 |
<?php //電話番号をポストするフォームを生成 echo<<<EOF <html> <meta charset="utf-8"> <head><title>電話番号に適切なハイフンを入れる</title> </head> <body> <form method="POST"> <textarea name="tels" placeholder="ハイフン無し電話番号を改行区切りで" cols=40 rows=4></textarea> <input type="submit" value="ハイフンを入れる"> </form> </body> </html> EOF; //市外局番の配列(総務省のHPからダウンロード) $arr_area_code = array("09969","09913","09912","09802","09496","08514","08512","08477","08396","08388","08387","07468","05979","05769","04998","04996","04994","04992","01658","01656","01655","01654","01648","01635","01634","01632","01587","01586","01564","01558","01547","01466","01457","01456","01398","01397","01392","01377","01374","01372","01267","0997","0997","0997","0997","0997","0996","0996","0995","0995","0994","0994","0993","0993","0987","0986","0985","0984","0983","0982","0982","0982","0980","0980","0980","0979","0978","0978","0978","0977","0974","0974","0973","0973","0972","0972","0969","0968","0968","0967","0967","0967","0966","0966","0965","0964","0959","0959","0959","0957","0957","0956","0955","0955","0954","0954","0952","0950","0949","0948","0947","0946","0944","0943","0943","0942","0940","0930","0920","0920","0920","0898","0897","0897","0896","0895","0895","0894","0894","0893","0892","0889","0889","0887","0887","0887","0887","0885","0884","0884","0884","0883","0883","0883","0880","0880","0880","0880","0879","0879","0877","0875","0869","0869","0868","0868","0867","0867","0867","0866","0866","0866","0865","0865","0863","0859","0859","0858","0858","0857","0856","0856","0855","0855","0855","0854","0854","0854","0854","0853","0852","0848","0847","0847","0847","0846","0846","0845","0838","0837","0837","0836","0835","0834","0833","0829","0827","0826","0826","0826","0824","0824","0823","0820","0820","0799","0799","0798","0797","0796","0796","0795","0795","0794","0791","0791","0790","0790","0779","0778","0776","0774","0773","0773","0772","0772","0771","0771","0770","0770","0768","0768","0767","0767","0766","0765","0763","0761","0761","0749","0749","0748","0748","0747","0747","0746","0746","0745","0745","0744","0743","0742","0740","0739","0738","0737","0736","0736","0735","0735","0725","0721","0599","0599","0598","0598","0597","0597","0596","0595","0595","0594","0587","0586","0585","0584","0581","0578","0577","0576","0575","0575","0574","0574","0573","0573","0572","0569","0568","0567","0566","0565","0564","0563","0562","0561","0558","0558","0557","0556","0556","0555","0554","0553","0551","0550","0548","0547","0545","0544","0539","0538","0537","0536","0536","0533","0532","0531","0495","0494","0493","0480","0479","0479","0478","0476","0475","0475","0470","0470","0467","0466","0465","0463","0460","0439","0438","0436","0428","0422","0299","0299","0297","0297","0296","0296","0295","0295","0294","0293","0291","0289","0288","0287","0287","0287","0285","0285","0284","0283","0282","0280","0279","0279","0278","0277","0276","0274","0274","0270","0269","0269","0268","0267","0267","0266","0265","0265","0264","0263","0261","0260","0259","0258","0257","0256","0256","0255","0254","0254","0254","0250","0248","0248","0247","0247","0246","0244","0243","0242","0241","0241","0241","0241","0240","0238","0238","0237","0237","0235","0234","0233","0229","0228","0226","0225","0224","0224","0223","0220","0198","0198","0197","0197","0195","0195","0194","0194","0193","0193","0192","0191","0187","0187","0186","0186","0186","0185","0185","0184","0183","0182","0179","0178","0176","0175","0175","0174","0173","0173","0172","0167","0166","0165","0164","0164","0164","0163","0163","0162","0158","0158","0158","0157","0156","0156","0155","0154","0153","0153","0153","0153","0152","0152","0152","0146","0146","0145","0145","0144","0143","0142","0139","0139","0138","0137","0137","0136","0136","0135","0135","0134","0133","0133","0126","0125","0124","0123","0123","0123","099","099","098","097","096","095","093","092","092","089","088","088","087","086","086","086","084","084","083","083","083","082","082","079","079","079","079","078","077","076","076","075","073","072","072","072","072","072","072","059","059","058","055","055","054","053","052","049","048","048","048","048","047","047","046","046","045","044","043","042","042","042","042","042","042","029","029","028","027","027","026","025","025","025","025","025","025","025","024","024","023","022","019","018","017","015","015","011","06","04","04","04","03",""); //POSTでtelsが来なかったら終了。 if(isset($_POST['tels'])){ $post_tel = $_POST['tels']; }else{ exit(); } //改行区切りで配列化 $arr_tel = explode(PHP_EOL, $post_tel); //ハイフンを入れる電話番号の分だけループ for($j=0; $j<count($arr_tel); $j++){ //半角数字のみにする $tel = ereg_replace("[^0-9]", "", $arr_tel[$j]); //サニタイズもする(不要?) $tel = htmlentities($tel); //11桁なら携帯電話番号なので、090-1234-5678って感じに if(strlen($tel) === 11){ $output_tel = substr($tel,0,3) ."-". substr($tel,3,4) ."-". substr($tel,6,4); //画面に表示して、次の電話番号 echo $output_tel . "<br />"; continue; } //市外局番の数だけループ for($i=0; $i<count($arr_area_code); $i++){ //前方一致でヒットしたら if(strpos($tel, $arr_area_code[$i]) === 0){ //市外局番の桁数を取得 $area_code_size = strlen($arr_area_code[$i]); //固定電話番号は10桁かつ、最後の4桁が個別番号なので、計算してハイフンを挿入 $output_tel = $arr_area_code[$i] ."-". substr($tel,$area_code_size,6-$area_code_size) ."-". substr($tel,6,4); //画面に表示して、次の電話番号 echo $output_tel . "<br />"; break; } } //対応する市外局番がなかったら if(count($arr_area_code) === $i) echo $tel."(該当の市外局番無し!)<br />"; } |