如何添加号码" 1"在PHP中作为电话号码的前缀?

时间:2016-06-06 21:46:48

标签: php crm vtiger

我想添加第一个" 1"在电话号码前面变量$ toNumbers。我试过PHP字符串

$toNumbers = '1'.$toNumbers;

要在电话号码前加1,但无法使其正常工作。我在这里做错了什么?

以下是来自vTiger短信模块,我需要格式化数字格式,前面有一个1才能发送。我想在每个数字前添加1,以便sms模块正确发送。问题是我的所有电话号码都存储为(xxx)xxx-xxxx而不是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("\n", $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 个答案:

答案 0 :(得分:1)

$toNumbers是一个数组时,你必须处理这个案例,试试这个:

if(!is_array($toNumbers)) 
d{
    $toNumbers = '1'.$toNumbers;
    $toNumbers = array($toNumbers);
}
else
{
    foreach ($toNumbers as $key => $field) 
         $toNumbers[$key] = '1'.$toNumbers[$key];
}

答案 1 :(得分:0)

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

甚至进入这段代码? $ toNumbers已经是一个数组吗? (你可能想要以任何一种方式处理这种情况。)