PHP 程式語言的 PHP wordwrap() 函數實際上是一個內建函數,它可以藉助 wordwrap() 函數內部的換行符將字串包裝成許多字元。它僅適用於 PHP 4.0.2 版本,也適用於其他以上 PHP 版本以支援此功能。 PHP wordwrap() 函數有助於在字串達到給定長度時將其換行成許多新行。在 PHP 程式語言和其他一些語言中處理字串函數時,該函數是最好的函數之一。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
wordwrap($str1, $width1, $break1, $cut1)
說明:
PHP 程式語言的 wordwrap() 函數通常接受上面語法部分提到的四個參數。
傳回值:
wordwrap() 函數有助於傳回一個包裝到指定長度的字串。它只不過是一個字串,成功時將被分成多行,失敗時將被分成 FALSE 值。
PHP程式語言的wordwrap()函數主要基於wordwrap()函數內部提到的四個參數。它的工作原理是使用中斷字符“break character”將給定的字符串包裝成多個字符,但這僅適用於 PHP 4.0.2 和其他以上 PHP 版本。以下是要實作的範例:
這是實作 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"; ?>
輸出:
這個範例也是了解 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); ?>
輸出:
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:
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:
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:
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中文網其他相關文章!