Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检测发送文本所需短信数量的最佳方法_Php_Encoding_Sms_Ascii_Ucs2 - Fatal编程技术网

Php 检测发送文本所需短信数量的最佳方法

Php 检测发送文本所需短信数量的最佳方法,php,encoding,sms,ascii,ucs2,Php,Encoding,Sms,Ascii,Ucs2,我正在寻找php中的代码/库,我将调用它并向其传递文本,它将告诉我: 我需要使用什么编码才能将此文本作为SMS发送(7,8,16位) 我将使用多少条短信发送此文本(如中所示,计算“分段信息”必须是明智的) 你知道有什么代码/库可以帮我实现这一点吗 再次,我不是要发送短信或转换短信,只是给我有关文本的信息 更新: 好的,我做了下面的代码,它似乎工作得很好,如果您有更好的/优化的代码/解决方案/库,请告诉我 $text = '\@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ -./012

我正在寻找php中的代码/库,我将调用它并向其传递文本,它将告诉我:

  • 我需要使用什么编码才能将此文本作为SMS发送(7,8,16位)
  • 我将使用多少条短信发送此文本(如中所示,计算“分段信息”必须是明智的)
  • 你知道有什么代码/库可以帮我实现这一点吗

    再次,我不是要发送短信或转换短信,只是给我有关文本的信息

    更新:

    好的,我做了下面的代码,它似乎工作得很好,如果您有更好的/优化的代码/解决方案/库,请告诉我

    $text = '\@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ -./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}[~]|€' ; //"\\". //'"';//' ';
    
    print $text . "\n";
    print isGsm7bit($text). "\n";
    print getNumberOfSMSsegments($text). "\n";
    
    
    
    
    function getNumberOfSMSsegments($text,$MaxSegments=6){
    /*
    http://en.wikipedia.org/wiki/SMS
    
    Larger content (concatenated SMS, multipart or segmented SMS, or "long SMS") can be sent using multiple messages, 
    in which case each message will start with a user data header (UDH) containing segmentation information. 
    Since UDH is part of the payload, the number of available characters per segment is lower: 
    153 for 7-bit encoding, 
    134 for 8-bit encoding and 
    67 for 16-bit encoding. 
    The receiving handset is then responsible for reassembling the message and presenting it to the user as one long message. 
    While the standard theoretically permits up to 255 segments,[35] 6 to 8 segment messages are the practical maximum, 
    and long messages are often billed as equivalent to multiple SMS messages. See concatenated SMS for more information. 
    Some providers have offered length-oriented pricing schemes for messages, however, the phenomenon is disappearing.
    */
    $TotalSegment=0;
    $textlen = mb_strlen($text);
    if($textlen==0) return false; //I can see most mobile devices will not allow you to send empty sms, with this check we make sure we don't allow empty SMS
    
    if(isGsm7bit($text)){ //7-bit
        $SingleMax=160;
        $ConcatMax=153;
    }else{ //UCS-2 Encoding (16-bit)
        $SingleMax=70;
        $ConcatMax=67;
    }
    
    if($textlen<=$SingleMax){
        $TotalSegment = 1;
    }else{
        $TotalSegment = ceil($textlen/$ConcatMax);
    }
    
    if($TotalSegment>$MaxSegments) return false; //SMS is very big.
    return $TotalSegment;
    }
    
    function isGsm7bit($text){
    $gsm7bitChars = "\\\@£\$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}[~]|€";
    $textlen = mb_strlen($text);
    for ($i = 0;$i < $textlen; $i++){
        if ((strpos($gsm7bitChars, $text[$i])==false) && ($text[$i]!="\\")){return false;} //strpos not able to detect \ in string
    }
    return true;
    }
    
    $text='\@$%;?"ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖܧ?abcdefghijklmnopqrstuvxyzäñ?//"\\". //'"';//' ';
    打印$text。“\n”;
    打印isGsm7bit($text)。“\n”;
    打印getNumberOfSMSsegments($text)。“\n”;
    函数getNumberOfSMSsegments($text,$MaxSegments=6){
    /*
    http://en.wikipedia.org/wiki/SMS
    更大的内容(串联短信、多部分或分段短信或“长短信”)可以使用多条信息发送,
    在这种情况下,每条消息将以包含分段信息的用户数据头(UDH)开始。
    由于UDH是有效负载的一部分,因此每个段的可用字符数较低:
    153用于7位编码,
    134用于8位编码和
    67用于16位编码。
    然后,接收手机负责重新组装消息并将其作为一条长消息呈现给用户。
    虽然该标准理论上最多允许255段,但实际最大允许有[35]6到8段消息,
    长消息通常被视为等同于多条短消息。有关更多信息,请参阅连接的短消息。
    一些提供商已经为消息提供了以长度为导向的定价方案,然而,这种现象正在消失。
    */
    $TotalSegment=0;
    $textlen=mb_strlen($text);
    如果($textlen==0)返回false;//我可以看到大多数移动设备不允许您发送空短信,通过此检查,我们确保不允许发送空短信
    if(isGsm7bit($text)){//7位
    $SingleMax=160;
    $ConcatMax=153;
    }else{//UCS-2编码(16位)
    $SingleMax=70;
    $ConcatMax=67;
    }
    如果($textlen$MaxSegments)返回false;//SMS非常大。
    返回$TotalSegment;
    }
    函数isGsm7bit($text){
    $gsm7bitChars=“\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
    $textlen=mb_strlen($text);
    对于($i=0;$i<$textlen;$i++){
    如果((strpos($gsm7bitChars,$text[$i])==false)&($text[$i]!=“\\”){return false;}//strpos无法检测字符串中的\项
    }
    返回true;
    }
    
    到目前为止,我拥有的最佳解决方案是:

    $text = '\@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ -./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}[~]|€' ; //"\\". //'"';//' ';
    
    print $text . "\n";
    print isGsm7bit($text). "\n";
    print getNumberOfSMSsegments($text). "\n";
    
    function getNumberOfSMSsegments($text,$MaxSegments=6){
    /*
    http://en.wikipedia.org/wiki/SMS
    
    Larger content (concatenated SMS, multipart or segmented SMS, or "long SMS") can be sent using multiple messages, 
    in which case each message will start with a user data header (UDH) containing segmentation information. 
    Since UDH is part of the payload, the number of available characters per segment is lower: 
    153 for 7-bit encoding, 
    134 for 8-bit encoding and 
    67 for 16-bit encoding. 
    The receiving handset is then responsible for reassembling the message and presenting it to the user as one long message. 
    While the standard theoretically permits up to 255 segments,[35] 6 to 8 segment messages are the practical maximum, 
    and long messages are often billed as equivalent to multiple SMS messages. See concatenated SMS for more information. 
    Some providers have offered length-oriented pricing schemes for messages, however, the phenomenon is disappearing.
    */
    $TotalSegment=0;
    $textlen = mb_strlen($text);
    if($textlen==0) return false; //I can see most mobile devices will not allow you to send empty sms, with this check we make sure we don't allow empty SMS
    
    if(isGsm7bit($text)){ //7-bit
        $SingleMax=160;
        $ConcatMax=153;
    }else{ //UCS-2 Encoding (16-bit)
        $SingleMax=70;
        $ConcatMax=67;
    }
    
    if($textlen<=$SingleMax){
        $TotalSegment = 1;
    }else{
        $TotalSegment = ceil($textlen/$ConcatMax);
    }
    
    if($TotalSegment>$MaxSegments) return false; //SMS is very big.
    return $TotalSegment;
    }
    
    function isGsm7bit($text){
    $gsm7bitChars = "\\\@£\$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}[~]|€";
    $textlen = mb_strlen($text);
    for ($i = 0;$i < $textlen; $i++){
        if ((strpos($gsm7bitChars, $text[$i])==false) && ($text[$i]!="\\")){return false;} //strpos not     able to detect \ in string
    }
    return true;
    }
    
    $text='\@$%;?"ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖܧ?abcdefghijklmnopqrstuvxyzäñ?//"\\". //'"';//' ';
    打印$text。“\n”;
    打印isGsm7bit($text)。“\n”;
    打印getNumberOfSMSsegments($text)。“\n”;
    函数getNumberOfSMSsegments($text,$MaxSegments=6){
    /*
    http://en.wikipedia.org/wiki/SMS
    更大的内容(串联短信、多部分或分段短信或“长短信”)可以使用多条信息发送,
    在这种情况下,每条消息将以包含分段信息的用户数据头(UDH)开始。
    由于UDH是有效负载的一部分,因此每个段的可用字符数较低:
    153用于7位编码,
    134用于8位编码和
    67用于16位编码。
    然后,接收手机负责重新组装消息并将其作为一条长消息呈现给用户。
    虽然该标准理论上最多允许255段,但实际最大允许有[35]6到8段消息,
    长消息通常被视为等同于多条短消息。有关更多信息,请参阅连接的短消息。
    一些提供商已经为消息提供了以长度为导向的定价方案,然而,这种现象正在消失。
    */
    $TotalSegment=0;
    $textlen=mb_strlen($text);
    如果($textlen==0)返回false;//我可以看到大多数移动设备不允许您发送空短信,通过此检查,我们确保不允许发送空短信
    if(isGsm7bit($text)){//7位
    $SingleMax=160;
    $ConcatMax=153;
    }else{//UCS-2编码(16位)
    $SingleMax=70;
    $ConcatMax=67;
    }
    如果($textlen$MaxSegments)返回false;//SMS非常大。
    返回$TotalSegment;
    }
    函数isGsm7bit($text){
    $gsm7bitChars=“\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
    $textlen=mb_strlen($text);
    对于($i=0;$i<$textlen;$i++){
    如果((strpos($gsm7bitChars,$text[$i])==false)&($text[$i]!=“\\”){return false;}//strpos无法检测字符串中的\项
    }
    返回true;
    }
    
    我在这里添加了一些额外的信息,因为前面的答案不太正确

    这些问题是:
    • 您需要将当前字符串编码指定为mb_字符串,否则可能会错误地收集到该字符串
    • 在7位GSM编码中,基本字符集扩展字符(^{}\[~]|€)需要每个14位进行编码,因此每个字符计为两个字符
    • 在UCS-2编码中,您必须小心表情符号和16位BMP以外的其他字符,因为
    • 带有UCS-2的GSM计数16位字符,因此如果您有
      • 第1页:160字节
      • 第2页:146字节
      • 第3页:153字节
      • 第4页:153字节
      • 第5页:153字节
      因此,无论使用何种语言:

      // strlen($text) show bytes 
      
                 $count = 0;
                 $len = strlen($text);
                      if ($len > 306) {
                          $len = $len - 306;
                          $count = floor($len / 153) + 3;
                      } else if($len>160){
                          $count = 2;
                      }else{
                          $count = 1;
                      }
      

      你的答案看起来不错。考虑把它作为一个答案,并批准它从“未回答”的问题中删除问题。你们没有考虑到,扩展的GSM符号应该被算作两个符号。所以在一些资料中我发现