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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化