PHPTS:一键免费搭建 Nginx + PHP + MySQL 运行环境

wordwrap

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

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

说明

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

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

参数

str

输入字符串。

width

列宽度。

break

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

cut

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

返回值

返回打断后的字符串。

范例

Example #1 wordwrap() 范例

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

echo 
$newtext;
?>

以上例程会输出:

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

Example #2 wordwrap() 范例

<?php
$text 
"A very long woooooooooooord.";
$newtext wordwrap($text8"\n"true);

echo 
"$newtext\n";
?>

以上例程会输出:

A very
long
wooooooo
ooooord.

Example #3 wordwrap() 例子

<?php
$text 
"A very long woooooooooooooooooord. and something";
$newtext wordwrap($text8"\n"false);

echo 
"$newtext\n";
?>

以上例程会输出:

A very
long
woooooooooooooooooord.
and
something

参见

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