PHP之string之wordwrap()函數使用

wordwrap

  • (PHP 4 >= 4.0.2, PHP 5, PHP 7)
  • wordwrap — Wraps a string to a given number of characters
  • wordwrap — 打斷字符串爲指定數量的字串

Description

string wordwrap ( 
    string $str [, 
    int $width = 75 [, 
    string $break = "\n" [, 
    bool $cut = FALSE ]]] 
    )
//Wraps a string to a given number of characters using a string break character.
//使用字符串斷點將字符串打斷爲指定數量的字串。

Parameters

str

  • The input string.
  • 輸入字符串。

width

  • The number of characters at which the string will be wrapped.
  • 列寬度。

break

  • The line is broken using the optional break parameter.
  • 使用可選的 break 參數打斷字符串。

cut

  • If the cut is set to TRUE, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example). When FALSE the function does not split the word even if the width is smaller than the word width.
  • 若是 cut 設置爲 TRUE,字符串老是在指定的 width 或者以前位置被打斷。所以,若是有的單詞寬度超過了給定的寬度,它將被分隔開來。(參見第二個範例)。 當它是 FALSE ,函數不會分割單詞,哪怕 width 小於單詞寬度。

Return Values

  • Returns the given string wrapped at the specified length.
  • 返回打斷後的字符串。

Examples

<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2018/3/8
 * Time: 下午9:27
 */

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

$text    = "A very long woooooooooooord.";
$newtext = wordwrap( $text, 8, "\n", false );
//A very
//long
//woooooooooooord.
echo $newtext . PHP_EOL;
$newtext = wordwrap( $text, 8, "\n", true );
//A very
//long
//wooooooo
//ooooord.
echo $newtext . PHP_EOL;

$str = "hello world hello php";
//hello-world-hello-php
echo wordwrap( $str, 5, "-", false ) . PHP_EOL;
//hello world hello php
echo wordwrap( $str ) . PHP_EOL;
//hello
//world
//hello
//php
echo wordwrap( $str, 3 ) . PHP_EOL;

See

All rights reserved

相關文章
相關標籤/搜索