title: best way to detect number of SMS needed to send a text
body: <p>I'm looking for a code/lib in php that I will call it and pass a text to it and it will tell me:</p>
  
  <ol>
  <li>What is the encode I need to use in order to send this text as SMS (7,8,16 bit)</li>
  <li>How many SMS message I will use to send this text (it must be smart to count "segmenation information" like in <a href="http://ozekisms.com/index.php?owpn=612" rel="noreferrer">http://ozekisms.com/index.php?owpn=612</a>)</li>
  </ol>
  
  <p>do you have any idea of any code/lib exists that will do this for me?</p>
  
  <p>Again I'm not looking for sending SMS or converting SMS, just to give me information about the text</p>
  
  <p><strong>Update:</strong> </p>
  
  <p>Ok I did the below code and it seems to be working fine, let me know if you have better/optimized code/solution/lib</p>
  
  <pre><code>$text = '\@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ -./0123456789:;&lt;=&gt;?¡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&lt;=$SingleMax){
      $TotalSegment = 1;
  }else{
      $TotalSegment = ceil($textlen/$ConcatMax);
  }
  
  if($TotalSegment&gt;$MaxSegments) return false; //SMS is very big.
  return $TotalSegment;
  }
  
  function isGsm7bit($text){
  $gsm7bitChars = "\\\@£\$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&amp;'()*+,-./0123456789:;&lt;=&gt;?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑÜ§¿abcdefghijklmnopqrstuvwxyzäöñüà^{}[~]|€";
  $textlen = mb_strlen($text);
  for ($i = 0;$i &lt; $textlen; $i++){
      if ((strpos($gsm7bitChars, $text[$i])==false) &amp;&amp; ($text[$i]!="\\")){return false;} //strpos not able to detect \ in string
  }
  return true;
  }
  </code></pre>