= 4.0.2, PHP 5, PHP 7, PHP 8)wordwrap — 打斷字符串為指定數(shù)量的字串說明wordwrap( string $str, int $width = 75, string $break = " "="" bool="" \n",="">

wordwrap

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

wordwrap打斷字符串為指定數(shù)量的字串

說明

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

使用字符串?dāng)帱c(diǎn)將字符串打斷為指定數(shù)量的字串。

參數(shù)

str

輸入字符串。

width

列寬度。

break

使用可選的 break 參數(shù)打斷字符串。

cut

如果 cut 設(shè)置為 true,字符串總是在指定的 width 或者之前位置被打斷。因此,如果有的單詞寬度超過了給定的寬度,它將被分隔開來。(參見第二個范例)。 當(dāng)它是 false ,函數(shù)不會分割單詞,哪怕 width 小于單詞寬度。

返回值

返回打斷后的字符串。

范例

示例 #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.

示例 #2 wordwrap() 范例

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

echo 
"$newtext\n";
?>

以上例程會輸出:

A very
long
wooooooo
ooooord.

示例 #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 換行標(biāo)記
  • chunk_split() - 將字符串分割成小塊