简体   繁体   English

如何使用PHP计算包括空格在内的字符,然后在一定长度后使用新行换行

[英]How to count characters including white space and then break onto a new line after a certain length using PHP

如何计算包括空格在内的字符,然后在一定长度后中断,例如,如何使用PHP将25个字符后的字符串中断到新行上?

Fortunately somebody's already done the work. 幸运的是,已经有人完成了工作。 Use wordwrap . 使用自动wordwrap


If you really want to reinvent the wheel for learning sake, here are a few pieces to get you started: 如果您真的想为学习而重塑方向,以下是一些入门指南:

  • for (...) { }
  • strlen()
  • $str[$x] to access character x of string $str $str[$x]访问字符串$str字符x
  • %
  • .

Try chunk_split () if you don't mind cutting words in half. 如果您不介意将单词切成两半,请尝试chunk_split ()。 It treats whitespace as any other char. 它将空格与其他任何char一样对待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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