wordwrap 打断字符串为指定数量的字串

发表日期:2021-07-01 10:23:26 | 来源: | | 浏览(928) 分类:字符串 函数

wordwrap

(PHP 4 >= 4.0.2, PHP 5, PHP 7, PHP 8)

wordwrap打断字符串为指定数量的字串

说明

wordwrap(
    string $str,
    int $width = 75,
    string $break = "\n",
    bool $cut = false
): string

使用字符串断点将字符串打断为指定数量的字串。

参数

str

输入字符串。

width

列宽度。

break

使用可选的 break 参数打断字符串。

cut

如果 cut 设置为 true,字符串总是在指定的 width 或者之前位置被打断。因此,如果有的单词宽度超过了给定的宽度,它将被分隔开来。(参见第二个范例)。 当它是 false ,函数不会分割单词,哪怕 width 小于单词宽度。

返回值

返回打断后的字符串。

范例

示例 #1 wordwrap() 范例

<?php 
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");
echo $newtext;
?>

以上例程会输出:

The quick brown fox<br />
jumped over the lazy<br />
dog.

示例 #2 wordwrap() 范例

<?php 
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", true);
echo "$newtext\n";
?>

以上例程会输出:

A very
long
wooooooo
ooooord.

示例 #3 wordwrap() 例子

<?php 
$text = "A very long woooooooooooooooooord. and something";
$newtext = wordwrap($text, 8, "\n", false);
echo "$newtext\n";
?>

以上例程会输出:

A very
long
woooooooooooooooooord.
and
something

参见

  • nl2br() - 在字符串所有新行之前插入 HTML 换行标记
  • chunk_split() - 将字符串分割成小块

集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。