Q:

Parse PHP String Based On Number of Characters

I'm starting to work on a small script that takes a string, counts the number of characters, then, based on the number of characters, splits/breaks the string apart and sends/emails 110 characters at a time.
What would be the proper logic/PHP to use to:
1) Count the number of characters in the string
2) Preface each message with (1/3) (2/3) (3/3), etc...
3) And only send 110 characters at a time.

I know I'd probably have to use strlen to count the characters, and some type of loop to loop through, but I'm not quite sure how to go about it.
Thanks!

A:

You could use str_split, if you're not concerned with where you break the strings.
Else, if you are concerned with this (and want to, say, split only on a whitespace), you could do something like:
// $str is the string you want to chop up.
$split = preg_split('/(.{0,110})\s/',
                    $str,
                    0,
                    PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

With this array you could then simply do:
$count = count($split);
foreach ($split as $key => $message) {
    $part = sprintf("(%d/%d) %s", $key+1, $count, $message);
    // $part is now one of your messages;
    // do what you wish with it here.
}

