PHP 自動換行()

WBOY
發布: 2024-08-29 12:53:36
原創
651 人瀏覽過

PHP 程式語言的 PHP wordwrap() 函數實際上是一個內建函數,它可以藉助 wordwrap() 函數內部的換行符將字串包裝成許多字元。它僅適用於 PHP 4.0.2 版本,也適用於其他以上 PHP 版本以支援此功能。 PHP wordwrap() 函數有助於在字串達到給定長度時將其換行成許多新行。在 PHP 程式語言和其他一些語言中處理字串函數時,該函數是最好的函數之一。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法

wordwrap($str1, $width1, $break1, $cut1)
登入後複製

說明:

PHP 程式語言的 wordwrap() 函數通常接受上面語法部分提到的四個參數。

  • $str1: wordwrap() 函數的 $str1 參數實際上有助於指定實際上需要分成多行的輸入字串。
  • $width1: wordwrap() 函數的 $width1 參數實際上有助於指定字串必須/將要換行的字元數。這些是字串值將在之後中斷的字元數。
  • $break1: wordwrap() 函數的 $break1 參數實際上是一個可選參數,但如果指定了它,它將在字串中斷的確切位置附加值。
  • $cut1: wordwrap() 函數的 $cut1 參數其實是一個布林參數。如果此 $cut1 參數值設定為 TRUE 值,則字串將始終在該點或在確切的指定寬度之前換行。這意味著如果它位於實際指定 width1 參數的限制的中間,它也會破壞之間的單字。如果這個 cut1 參數設定為 FALSE 值,那麼 wordwrap() 函數甚至不會分割單字,即使寬度參數的值小於單字的寬度。

傳回值:

wordwrap() 函數有助於傳回一個包裝到指定長度的字串。它只不過是一個字串,成功時將被分成多行,失敗時將被分成 FALSE 值。

wordwrap() 函數在 PHP 中如何運作?

PHP程式語言的wordwrap()函數主要基於wordwrap()函數內部提到的四個參數。它的工作原理是使用中斷字符“break character”將給定的字符串包裝成多個字符,但這僅適用於 PHP 4.0.2 和其他以上 PHP 版本。以下是要實作的範例:

範例#1

這是實作 PHP 程式語言的 wordwrap() 函數的範例。首先,使用字串值「My Name is Pavan Kumar Sake.」建立變數「$text1」。然後再次建立另一個變數「$newtext1」變數來儲存 wordwrap() 函數及其四個參數。在這個 wordwrap() 函數中,$str1 參數是 $text 變數的值。然後使用 8 作為寬度參數,用於 html 標籤「
」之一的換行符號使用時,對於 $cut1 參數,傳遞 TRUE 值。然後,每 8 個字元就會將單字換行,然後將其列印出來。此列印是藉助 PHP 網路程式語言的 echo 函數完成的。

代碼:

<?php
$text1 = "My Name is Pavan Kumar Sake.";
$newtext1 = wordwrap($text1, 8, "<br>", TRUE);
echo "$newtext1";
?>
登入後複製

輸出:

PHP 自動換行()

範例#2

這個範例也是了解 wordwrap() 函數如何在上面語法中提到的一些參數的幫助下工作的實作之一。首先,建立變數“$stringn1”並儲存字串句子值。然後width1變數被儲存為「10」值,break1變數被建立為「
」 html 標籤值。然後在echo語句之後使用wordwrap()函數來列印wordwrap()函數的結果。這裡一些字串是透過寬度“10”換行來建立的。

代碼:

<?php
$stringn1 = "Pavan Kumar Sake a Blogger PhysicsNerd and BusinessMan";
$width1 = 10;
$break1 = "</br>";
echo wordwrap($stringn1, $width1, $break1);
?>
登入後複製

輸出:

PHP 自動換行()

Example #3

This example also similar to the above example 1. At first $stringn1, $width1, $break1 variables are created with string value, integer value ,
tag value. Then wordwrap() function is used but $cut1 parameter is not used here. If the word’s width is larger than the specified width parameter value then the whole word will be printed by wrapping at first only if the $cut1 parameter is not used.

Code:

<?php
$stringn1 = "Congratulations! Pavan Sake kumar";
$width1 = 8;
$break1 = "</br>";
echo wordwrap($stringn1, $width1, $break1);
?>
登入後複製

Output:

PHP 自動換行()

Example #4

This program is also one of the wordwrap() function implementation by mentioning the $cut1 parameter by passing TRUE value to it. Here a variable “$stringnn1” is created with string value then $width11 variable is created with 8 as value then $break11 variable is created with
tag as break parameter, $cut1 variable is created with TRUE as value. Then wordwrap() function is used after the echo statement. Here cut1 parameter is used inside of the function by passing TRUE as value. Even though the string width is bigger than the width11 value then word will be broken and wrapped into single string text exactly. You can check the output below.

Code:

<?php
$stringnn1 = "Congratulations! Power Ranger of the World";
$width11 = 8;
$break11 = "</br>";
$cut11 = true;
echo wordwrap($stringnn1, $width11, $break11, $cut11);
?>
登入後複製

Output:

PHP 自動換行()

Example #5

This program is so similar to the example 4 but here $cut1 parameter value is passed with the FALSE value and stringnn1 variable is passed new string value, width parameter is mentioned with 7 as value. So the output will be different. For bigger strings or smaller strings, whole strings will be printed after some spaces like that. But most of the time the width of the strings which are wrapped will be of the length “7”.

Code:

<?php
$stringnn1 = "Be a part of Game Changer :: profitloops";
$width11 = 7;
$break11 = "</br>";
$cut11 = false;
echo wordwrap($stringnn1, $width11, $break11, $cut11);
?>
登入後複製

Output:

PHP 自動換行()

Conclusion

I hope you learnt what is the definition of PHP wordwrap() function along with its syntax with all the parameters explanation, How the wordwrap() function works in PHP Programming language along with various examples of implementing the wordwrap() function to understand the wordwrap() function better.

以上是PHP 自動換行()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!