在长文本中自动换行 - 照顾句子
Make auto line breaks in a long text - take care of sentences
一个字符串的文本很长。如何在 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 只是印刷和排版行业的虚拟文本。自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商拿走了一个字体厨房,并把它加在一起制作了一本字体样本书。它不仅经历了五个世纪,而且还经历了电子排版的飞跃,基本保持不变。
它在 1960 年代随着包含 Lorem Ipsum 段落的 Letraset 工作表的发布而流行,最近随着桌面出版软件如 Aldus PageMaker 包括 Lorem 版本而流行 Ipsum.Lorem Ipsum 只是打印和打印的虚拟文本排版业。自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商拿走了一个字体厨房,并把它加在一起制作了一本字体样本书。
它不仅经历了五个世纪,而且还经历了电子排版的飞跃,基本保持不变。它在 1960 年代随着包含 Lorem Ipsum 段落的 Letraset 工作表的发布而流行,最近随着桌面出版软件如 Aldus PageMaker 的发布而流行,其中包括 Lorem Ipsum 的版本。
我试过了wordwrap
但是太简单了
用空格分割字符串。将项目一项一项添加到输出中,同时保留一个计数器。如果计数器 >= 100,检查每个单词是否以点结尾。如果是,则输出一个 break 并将计数器重置为 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>';
一个字符串的文本很长。如何在 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 只是印刷和排版行业的虚拟文本。自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商拿走了一个字体厨房,并把它加在一起制作了一本字体样本书。它不仅经历了五个世纪,而且还经历了电子排版的飞跃,基本保持不变。
它在 1960 年代随着包含 Lorem Ipsum 段落的 Letraset 工作表的发布而流行,最近随着桌面出版软件如 Aldus PageMaker 包括 Lorem 版本而流行 Ipsum.Lorem Ipsum 只是打印和打印的虚拟文本排版业。自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商拿走了一个字体厨房,并把它加在一起制作了一本字体样本书。
它不仅经历了五个世纪,而且还经历了电子排版的飞跃,基本保持不变。它在 1960 年代随着包含 Lorem Ipsum 段落的 Letraset 工作表的发布而流行,最近随着桌面出版软件如 Aldus PageMaker 的发布而流行,其中包括 Lorem Ipsum 的版本。
我试过了wordwrap
但是太简单了
用空格分割字符串。将项目一项一项添加到输出中,同时保留一个计数器。如果计数器 >= 100,检查每个单词是否以点结尾。如果是,则输出一个 break 并将计数器重置为 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>';