Formatting Phone Numbers in PHP

前端 未结 20 2298
面向向阳花
面向向阳花 2020-11-28 19:39

I am working on an SMS app and need to be able to convert the sender\'s phone number from +11234567890 to 123-456-7890 so it can be compared to re

20条回答
  •  旧时难觅i
    2020-11-28 19:46

    Please have a look at substr based function that can change formats

    function phone(string $in): string
    {
        $FORMAT_PHONE = [1,3,3,4];
        $result =[];
        $position = 0;
        foreach ($FORMAT_PHONE as $key => $item){
            $result[] = substr($in, $position, $item);
            $position += $item;
        }
        return '+'.implode('-',$result);
    }
    

提交回复
热议问题