dtujfmfs06058 2018-10-15 10:59
浏览 48
已采纳

格式化电话号码(如果丢失则检测并添加国家/地区代码)

I have the following regex query that I built the idea is to take user input (phone numbers) and then make sure all the numbers are the exact same format when I store it in the database.

The problem I have is that this regex of mine does not cater for all the scenarios, I will explain in a bit.

This is my current code:

//format phone numbers
function Number_SA($number)
{
    //strip out everything but numbers
    $number =  preg_replace("/[^0-9]/", "", $number);
    //Strip out leading zeros:
    $number = ltrim($number, '0');
    //The default country code
    $default_country_code  = '+27';
    //Check if the number doesn't already start with the correct dialling code:
    if ( !preg_match('/^[+]'.$default_country_code.'/', $number)  ) {
        $number = $default_country_code.$number;
    }
    //return the converted number:
    return $number;
}

The behavior I want is the following; If a user enters any of the following formats the number should end up as +27

  • 797734809 #missing a leading 0
  • 0797734809
  • 27797734809
  • +27797734809

Should all be converted to:

  • +27797734809

PS: I first clean the number input with this:

function clean_input($data) 
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

And then I use the Number_SA($number) function on the number. In other words:

$number = clean_input($_POST["cell_number"]);
$msisdn = Number_SA($number);

PPS: Number_SA, the SA stands for South Africa since I live there and I want this to work for our country code which is +27

Solution, I ended up with @Kamil Kiełczewski answer by building it into my program as follows:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (empty($_POST["cell_numbers"])) {
        $err = "Please add numbers!";
    } else {
        $numbers = test_input($_POST["cell_numbers"]);
        $numbers = explode("
", str_replace("", "", $numbers));
        foreach ($numbers as $number) {
            $msisdn = preg_replace('/^(?:\+27|27|0)?/','+27', $number);
            //insert into the database
        }        
    }
}
  • 写回答

2条回答 默认 最新

  • duannong1801 2018-10-15 11:05
    关注

    Here is your regexp:

    ^(?:\+27|27|0)?(\d*)
    

    and if it match something use this: +27$1

    Here is working example (improved): https://regex101.com/r/VaUAKN/7

    EXPLANATION:

    First we wanna split prefix (if exists) with number: ^(?:\+27|27|0)? the ?: inside group makes that prefix group will be ignored in results. The ^ means begining of line. The last ? allow prefix to not exists. And at the end (\d*) catch numbers after prefix

    PHP working example (Wiktor Stribiżew noted that in regexp we can can omit (\d*) and $1 and change \+27|27 to \+?27 ):

    $numOld = "2712345678";
    $num = preg_replace('/^(?:\+?27|0)?/','+27', $numOld); // > $num = "+2712345678"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?