160 character paragraph split for text messages

I am having problems with the logic of accepting a paragraph of text and splitting it into words / sentences for sending in multiple text messages. Each text message can contain up to 160 characters. I want to cleanly break the paragraph.

Here is the solution (thanks Leventix!):

public static function splitStringAtWordsUpToCharacterLimit($string, $characterLimit) {
    return explode("\n", wordwrap($string, $characterLimit));
}
+3
source share
4 answers

You can use wordwrap , then explode with newline characters.

+6
source

This is the function I use

function sms_chunk_split($msg) {
   $msg = preg_replace('/[\r\n]+/', ' ', $msg);
   $chunks = wordwrap($msg, 160, '\n');
   return explode('\n', $chunks);
}

It splits a long SMS message into an array of 160 byte fragments, dividing word boundaries.

+3
source

?

All you have to do is split the string into multiple text messages. so you would do something like (I cannot remember the exact syntax, my PHP is rusty) length($string)/$charmaxand then just a substring that gets into the array many times and returns that array

0
source
<?php
 $string = str_repeat('Welcome to StackOverFlow, Heres Your Example Code!', 6);

 print_r(str_split($string, 160));

 # You could also Alias the function.
 function textMsgSplit($string, $splitLen = 160) {
  return str_split($string, $splitLen);
 }
?>
0
source

Source: https://habr.com/ru/post/1722815/


All Articles