Split string every 80 characters in PHP
I'm pulling data from a mainframe. I have this long string. I need to make a print view of this string. If I split it every 80 characters it will format correctly (since that's how it's开发者_C百科 chopped up on the mainframe screen). By splitting I mean inserting an end of line or a break in the view.
The code is simple:
$string = "A big long string that needs to be chopped every 80 chars";
Use the following:
chunk_split ( $string, 80 );
There's a 3rd parameter, used to set the separator. By default, this is a platform-specific end-of-line.
This is probably faster than wordwrap
since it doesn't take care of words at all.
Try the wordwrap function.
Use wordwrap()
:
http://php.net/manual/en/function.wordwrap.php
wordwrap($string, 80, "\n", true);
Substitute <br>
for \n
depending on your needs.
精彩评论