dongyuan1984 2016-06-06 21:46
浏览 53
已采纳

如何在PHP中添加数字“1”作为电话号码的前缀?

I want to add the number one "1" in front of the phone numbers variable $toNumbers. I have tried the PHP string

$toNumbers = '1'.$toNumbers;

To append the 1 before the phone numbers but can't get it to work. What am I doing wrong here?

The below is from the vTiger SMS module and I need to format the number format with a 1 in front of it in order to send. I want to add the 1 in front of every number for the sms module to send properly. The issue is that all my phone numbers are stored as (xxx)xxx-xxxx instead of 1 (xxx)xxx-xxxx.

public function send($message, $toNumbers) {

    if(!is_array($toNumbers)) {
        $toNumbers = '1'.$toNumbers;
        $toNumbers = array($toNumbers);
    }


    $params = $this->prepareParameters();

    $params['text'] = $message;
    $params['to'] = implode(',', $toNumbers);


    $serviceURL = $this->getServiceURL(self::SERVICE_SEND);
    $httpClient = new Vtiger_Net_Client($serviceURL);
    $response = $httpClient->doPost($params);
    $responseLines = split("
", $response);

    $results = array();
    $i=0;
    foreach($responseLines as $responseLine) {
        $responseLine = trim($responseLine);
        if(empty($responseLine)) continue;

        $result = array( 'error' => false, 'statusmessage' => '' );
        if(preg_match("/ERR:(.*)/", trim($responseLine), $matches)) {
            $result['error'] = true;
            $result['to'] = $toNumbers[$i++];
            $result['statusmessage'] = $matches[0]; // Complete error message
        } else if(preg_match("/ID: ([^ ]+)TO:(.*)/", $responseLine, $matches)) {
            $result['id'] = trim($matches[1]);
            $result['to'] = trim($matches[2]);
            $result['status'] = self::MSG_STATUS_PROCESSING;
        } else if(preg_match("/ID: (.*)/", $responseLine, $matches)) {
            $result['id'] = trim($matches[1]);
            $result['to'] = $toNumbers[0];
            $result['status'] = self::MSG_STATUS_PROCESSING;
        } 
        $results[] = $result;
    } 
    return $results;
} 
  • 写回答

2条回答 默认 最新

  • doumao1519 2016-06-06 23:10
    关注

    You must handle the case when $toNumbers is an array, try this :

    if(!is_array($toNumbers)) 
    d{
        $toNumbers = '1'.$toNumbers;
        $toNumbers = array($toNumbers);
    }
    else
    {
        foreach ($toNumbers as $key => $field) 
             $toNumbers[$key] = '1'.$toNumbers[$key];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作