mow*_*gli 15 php line-breaks word-wrap
我正在寻找一种方法让PHP中的自动换行更智能一点.因此,它不会预先打破长字,只留下任何先前的小字.
假设我有这个(真正的文本总是完全动态的,这只是为了显示):
wordwrap('hello! heeeeeeeeeeeeeeereisaverylongword', 25, '<br />', true);
Run Code Online (Sandbox Code Playgroud)
这输出:
你好!
heeeeeeeeeeeeeeeeeeeveryvery
longword
看,它在第一行留下了单词.我怎样才能让它输出更像这样的东西:
你好!heeeeeeeeeeee
eeereisaverylongword
因此它利用每条线上的任何可用空间.我已经尝试了几个自定义函数,但没有一个是有效的(或者它们有一些缺点).
cmb*_*ley 16
我已经开始使用这个智能文字包装的自定义功能了:
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";
Run Code Online (Sandbox Code Playgroud)
编辑:发现了一些警告.对此(以及本机功能)的一个主要警告是缺少多字节支持.
frg*_*lps 13
怎么样
$string = "hello! heeeeeeeeeeeeeeereisaverylongword";
$break = 25;
echo implode(PHP_EOL, str_split($string, $break));
Run Code Online (Sandbox Code Playgroud)
哪个输出
hello! heeeeeeeeeeeeeeere
isaverylongword
Run Code Online (Sandbox Code Playgroud)
str_split()将字符串转换为$ break size块的数组.
implode()使用胶水将数组作为字符串连接在一起,在这种情况下,胶水是行结束标记(PHP_EOL),尽管它可以很容易地成为' <br/>
'
mow*_*gli 12
这也是一个解决方案(对于浏览器等):
$string = 'hello! heeeeeeeeeeeeeeeeeeeeeereisaverylongword';
echo preg_replace('/([^\s]{20})(?=[^\s])/', '$1'.'<wbr>', $string);
Run Code Online (Sandbox Code Playgroud)
它放置一个<wbr>
包含20个或更多字符的单词
<wbr>
意思是"分词机会",所以它只有在必须时才会中断(由元素/浏览器/查看器/其他的宽度决定).否则它是看不见的.
适用于没有固定宽度的流畅/响应式布局.并不像php的wordwrap那样包裹奇怪
您可以使用CSS来完成此任务.
word-wrap: break-word;
Run Code Online (Sandbox Code Playgroud)
那会打破你的话.这是一个链接,可以看到它的实际效果:
http://www.css3.info/preview/word-wrap/
归档时间: |
|
查看次数: |
27981 次 |
最近记录: |