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中文网其他相关文章!