简体   繁体   中英

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 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.

尝试自动换行功能。

Use wordwrap() : http://php.net/manual/en/function.wordwrap.php

wordwrap($string, 80, "\n", true);

Substitute <br> for \\n depending on your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM