php时间换行_在PHP中更长时间的单词换行吗?

本文探讨了一个名为smart_wordwrap的自定义PHP函数,用于自动换行长字符串。函数在处理特定单词时可能出现问题,尤其是在遇到多字节字符(如UTF-8)时。博客作者指出了该函数的警告,并提出了解决多字节支持不足的挑战。示例代码展示了如何重新构造字符串以适应指定宽度,并进行多行换行。
摘要由CSDN通过智能技术生成

我已经去了这个智能wordwrap的自定义功能:

function smart_wordwrap($string, $width = 75, $break = "\n") {

// split on problem words over the line length

$pattern = sprintf('/([^ ]{%d,})/', $width);

$output = '';

$words = preg_split($pattern, $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

foreach ($words as $word) {

if (false !== strpos($word, ' ')) {

// normal behaviour, rebuild the string

$output .= $word;

} else {

// work out how many characters would be on the current line

$wrapped = explode($break, wordwrap($output, $width, $break));

$count = $width - (strlen(end($wrapped)) % $width);

// fill the current line and add a break

$output .= substr($word, 0, $count) . $break;

// wrap any remaining characters from the problem word

$output .= wordwrap(substr($word, $count), $width, $break, true);

}

}

// wrap the final output

return wordwrap($output, $width, $break);

}

$string = 'hello! too long here too long here too heeeeeeeeeeeeeereisaverylongword but these words are shorterrrrrrrrrrrrrrrrrrrr';

echo smart_wordwrap($string, 11) . "\n";

编辑:发现了几个警告.与此(以及本机功能)的一个主要注意事项是缺乏多字节支持.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值