16
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

phpの正規表現メモ 電話番号形式変換

Last updated at Posted at 2016-08-16

#特殊文字

特殊文字 意味
* 0回以上の繰り返し
+ 1回以上の繰り返し
? 0回または1回
{n} n回の繰り返し
{n,m} n回からm回の繰り返し
{n,} n回の繰り返し
^ 行頭
¥ 行末
\d 0-9の数字1字
\w アルファベットか数字かアンダースコア

#正規表現でマッチング


preg_match(正規表現パターン,対象文字列

//例
$input = "123-4567";
$pata = "/^\d{3}[-ー]?\d{4}$/";

if(preg_match($pata,$input)) {
	echo "$inputは郵便番号の形式です";
} else {
	echo "$inputは郵便番号の形式ではありません";
}

#正規表現で置換


preg_replace(正規表現パターン,変換形式,対象文字列

//例
$input = "2015/5/25"
$pata = "/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/";
$rep = "$1年$2月$3日";

$test = preg_replace($pata,$rep,$input);
// $test == 2015年5月25日

#電話番号置き換えプログラム
正規表現で電話番号の形式をチェックし、「-」の削除、復元を行う。電話番号のデータベースへの登録、取り出しの際に利用。

//電話番号のフォーマット
class phone_formater {

    //電話番号から「-」を削除
    public static function replace($input) {
        //「-」の空白への置き換え
        $search = array("-","ー");
        $result = str_replace($search,"",$input);
        return $result;
    }

    //電話番号に「-」を追加
    public static function format($input) {
        //変数宣言
        $category = array(
            "normal" => "/^0[^346]\d{8}$/",
            "mobile" => "/^\d{11}$/",
            "tokyo"  => "/^0[346]\d{7}$/",
            "none"   => "/^\d{7}$/",
        );
        $pattern = array(
            "normal" => "/(\d{3})(\d{3})(\d{4})/",
            "mobile" => "/(\d{3})(\d{4})(\d{4})/",
            "tokyo"  => "/(\d{2})(\d{3})(\d{4})/",
            "none"   => "/(\d{3})(\d{4})/",
        );
        $rep = array(
            "normal" => "$1-$2-$3",
            "none"   => "$1-$2",
        );

        //携帯ならら
        if(preg_match($category['mobile'],$input)) {
            $result = preg_replace($pattern['mobile'],$rep['normal'],$input);
        }
        //市外局番2桁なら
        elseif(preg_match($category['tokyo'],$input)) {
            $result = preg_replace($pattern['tokyo'],$rep['normal'],$input);
        }
        //普通の市外局番なら
        elseif(preg_match($category['normal'],$input)) {
            $result = preg_replace($pattern['normal'],$rep['normal'],$input);
        }
        //市外局番なしなら
        elseif(preg_match($category['none'],$input)) {
            $result = preg_replace($pattern['none'],$rep['none'],$input);
        }
        //その他なら
        else {
            $result = $input;
        }

        return $result;
    }
}

$input = 03-1234-5678;

//「-」の削除
$result1 = phone_format::replace($input);

//「-」の復元
$result2 = phone_format::format($result1);

//$result2 == 080-6520-7740
```
16
16
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?