字符串的文本很长。如何在没有剪切单词的情况下在100个单词之后进行自动换行并处理逗号和点。不应该打破一句话。点后只有换行符。如果在完整文本中没有br
标记,则应添加换行符。
示例:
$string = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';
输出:
Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是业界标准的虚拟文本,当时一台未知的打印机采用了类型的厨房,并将其拼凑成一本类型的样本。它不仅存在了五个世纪,而且还延续了电子排版,基本保持不变。
它在20世纪60年代推出了包含Lorem Ipsum段落的Letraset表格,最近发布了包括Lorem Ipsum版本在内的桌面出版软件Aldus PageMaker.Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是业界标准的虚拟文本,当时一台未知的打印机采用了类型的厨房,并将其拼凑成一本类型的样本。
它不仅存在了五个世纪,而且还延续了电子排版,基本保持不变。它在20世纪60年代推出了包含Lorem Ipsum段落的Letraset表格,最近还发布了包括Lorem Ipsum版本在内的桌面出版软件Aldus PageMaker。
我试过了wordwrap
,但这太简单了。
答案 0 :(得分:0)
按空格拆分字符串。在保留计数器的同时逐项添加项目到输出。如果计数器> = 100,检查每个单词是否以点结束。如果是,则输出中断并将计数器重置为0.
所以,像这样:
<?php
$string = 'Your chunk of. Lipsum.';
$words = explode(' ', $string);
echo '<p>';
$counter = 0;
foreach ($words as $word) {
echo $word . ' ';
if (++$counter >= 100) {
if (substr($word, -1) === '.') {
echo "</p>\n\n<p>"; // End the paragraph and start a new one.
$counter = 0;
}
}
}
echo '</p>';