Break a long line with PHP for outgoing SMS messages

My client is looking for a way to send a text message in a Twilio PHP script that I programmed and then passed it to the staff in the field. This is the simple part, just check if the incoming number is allowed, pull the staff details from MySQL and distribute.

Here's the tricky part - people using this can be long, and their phones allow them to enter more than 160 characters. Assuming Twilio can accept> 160 characters (I know it cannot send> 160), I need to break this long message (string) into chunks that fall under 160 characters.

Here is the script I came up with for this, it works great, but I would like it to finish the full word , and not just the next character after it is Crack. A funny story, when you forgot to enter the length with which you want to split the line, you get 171 text messages of one character !: P

<?php $string = "Ulysses, Ulysses - Soaring through all the galaxies. In search of Earth, flying in to the night. Ulysses, Ulysses - Fighting evil and tyranny, with all his power, and with all of his might. Ulysses - no-one else can do the things you do. Ulysses - like a bolt of thunder from the blue. Ulysses - always fighting all the evil forces bringing peace and justice to all."; $arr = str_split($string, 155); //Factoring in the message count $num = count($arr); //See how many chunks there are $i = 1; //Set interval to 1 foreach ($arr as $value) { if ($num > 1) { echo "($i/$num) $value<br/>"; //(x/x) Message... $i++; } else { echo $value; //Message... } } ?> 

Many thanks

CORRECTION Sorry, the main supervision on my part is that I posted a development script to play with strings and not with a live script that sends the actual SMS after the "incident". I need to be able to iterate over the pieces in order to send them as my own SMS after they are split, as will be done above, just ending with a full word. SMS is sent in a foreach loop.

+6
string php twilio
source share
4 answers

I don’t understand why all the more complex answers when you can just use:

 $chunks = explode("||||",wordwrap($message,155,"||||",false)); $total = count($chunks); foreach($chunks as $page => $chunk) { $message = sprintf("(%d/%d) %s",$page+1,$total,$chunk); } 

This will result in something like:

(1/3) Primis facilis apeirian vis ne. Idque ignota est ei. Ut sonet indoctum nam, ius ea lighting fabellas. Pri delicata percipitur ad, munere ponderum rationibus.

Online example: http://codepad.org/DTOpbPIJ Updated

+8
source share

you can explode () a string first, using space as a delimiter. Once you have an array, start moving around it and add the words to the line one by one. Check if the total length of the string will exceed 160 before adding a word to the string. If so, start a new line. You can do this by storing an array of strings.

 <?php $string = "Ulysses, Ulysses - Soaring through all the galaxies. In search of Earth, flying in to the night. Ulysses, Ulysses - Fighting evil and tyranny, with all his power, and with all of his might. Ulysses - no-one else can do the things you do. Ulysses - like a bolt of thunder from the blue. Ulysses - always fighting all the evil forces bringing peace and justice to all." $arr = explode(' ', $string); // explode by space $msgs = array(); // array of finished messages $i = 0; // position within messages array foreach($arr as $word) { if (strlen($msgs[$i] . $word) <= 155) // this may be 160, im not sure { $msgs[$i] .= $word; } else { $i++; $msgs[$i] = $word; } } ?> 
+1
source share

Try wordwrap: http://www.php.net/manual/en/function.wordwrap.php

 <?php $words = "this is a long sentence that needs splitting"; foreach(explode("\n", wordwrap($words, 10, "\n")) as $s) { echo $s . "\n"; } 
+1
source share

How about this (Note: untested, not at the top of my head, but you understand):

 $string = '<your long message>'; $parts = array(); while (strlen($string) > 155) { $part = substr($string, 0, 155); $lastSpace = strrpos($part, ' '); $parts[] = substr($string, 0, $lastSpace); $string = substr($string, $lastSpace); } $parts[] = $string; var_dump($parts); 
0
source share

All Articles