Skip to content
Advertisement

Splitting a Paragraph into 160 Character Pieces for Text Messaging

I’m having trouble with the logic of taking a paragraph of text and splitting it on words/sentences to send out in multiple text messages. Each text message can only have up to 160 characters. I want to cleanly break a paragraph up.

Here is the solution (thanks Leventix!):

public static function splitStringAtWordsUpToCharacterLimit($string, $characterLimit) {
    return explode("n", wordwrap($string, $characterLimit));
}

Advertisement

Answer

You can use wordwrap, then explode by newlines:

public static function splitStringAtWordsUpToCharacterLimit($string, $characterLimit) {
    return explode("n", wordwrap($string, $characterLimit));
} 
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement