实例
按照指定长度对字符串进行折行处理:
<?php
$str = "an example of a long word is: supercalifragulistic";
echo wordwrap($str,15,"<br>n");
?>
定义和用法
wordwrap() 函数按照指定长度对字符串进行折行处理。
注释:该函数可能会在行的开头留下空格。
语法
wordwrap(string,width,break,cut)
参数 描述
string 必需。规定要进行折行的字符串。
width 可选。规定最大行宽度。默认是 75。
break 可选。规定作为分隔符使用的字符(字串断开字符)。默认是 "\n"。
cut 可选。规定是否对大于指定宽度的单词进行折行:false - 默认。不折行
true - 折行
技术细节
返回值: 如果成功,则返回折行后的字符串。如果失败,则返回 false。
php 版本: 4.0.2+
更新日志: 在 php 4.0.3 中,新增了 cut 参数。
更多实例
实例 1
使用所有的参数:
<?php
$str = "an example of a long word is: supercalifragulistic";
echo wordwrap($str,15,"<br>n",true);
?>
实例 2
对字符串进行折行:
<?php
$str = "an example of a long word is: supercalifragulistic";
echo wordwrap($str,15);
?>
上面代码的 html 输出如下(查看源代码):
<!doctype html>
<html>
<body>
an example of a
long word is:
supercalifragulistic
</body>
</html>
上面代码的浏览器输出如下:
an example of a long word is: supercalifragulistic
wordwrap()函数可以按照指定的固定行长度格式化文本段落,让段落看起来更加整齐
<?php
$string = "trading on margin poses additional
risks and is not suitable for all
investors.
a complete list of the risks associated with margin trading is
available in the margin risk disclosure document.";
$string = str_replace("\n", " ", $string);
$string = str_replace("\r", " ", $string);
print(wordwrap($string, 40)."\n");
?>
上面的代码返回如下结果
trading on margin poses additional
risks and is not suitable for all
investors. a complete list of the
risks associated with margin trading is
available in the margin risk disclosure
document.
以上就是php按照指定长度对字符串进行折行处理的函数wordwrap()的详细内容。