简体   繁体   中英

PHP: Insert a substring into a HTML string after every N characters?

I have a long string that will be displayed on my page. I want it to have a line break after every 40 chars. How can I insert these?

Use the <br> or <wbr> tag:

wordwrap($string, 40, "<wbr>", true);

Basically, <wbr> represents a place where the browser is allowed to break the word on a new line, but it won't display a space if it doesn't have to.

<br> on the other hand will always create a line break.

使用chunk_split 手册

chunk_split($string, 40, "\n");
$string = 'loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string';
echo wordwrap($string, 40, "\n", true);

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