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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?