wordwrap

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

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

说明

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

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

参数

string

输入字符串。

width

列宽度。

break

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

cut_long_words

如果 cut_long_words 设置为 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() - 将字符串分割成小块
To Top