如何将 PHP 中的数字“1”作为前缀添加到 phone 号码?

How to add the number "1" in PHP as a Prefix to a phone number?

我想在 phone 数字变量 $toNumbers 前添加第一个“1”。我试过 PHP 字符串

$toNumbers = '1'.$toNumbers;

要在 phone 数字前附加 1 但无法正常工作。我在这里做错了什么?

下面是vTiger SMS模块,我需要在数字格式前加上1才能发送。我想在每个号码前添加 1,以便短信模块正确发送。问题是我所有的 phone 号码都存储为 (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;
} 
if(!is_array($toNumbers)) {
    $toNumbers = '1'.$toNumbers;
    $toNumbers = array($toNumbers);
}

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

你必须处理 $toNumbers 是数组的情况,试试这个:

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