<?php
function word_wrapper($text,$minWords = 3) {
$return = $text;
$arr = explode(' ',$text);
if(count($arr) >= $minWords) {
$arr[count($arr) - 2].= ' '.$arr[count($arr) - 1];
array_pop($arr);
$return = implode(' ',$arr);
}
return $return;
}
?>
您可以将其缩短为
= $minWords)
? ' ' . array_pop($output)
: ' ' . array_pop($output);
$output = implode(' ',$output) . $widow;
return $output;
}
?>
正是我的需求。感谢!
如何为所有少于 5 个字母的单词添加不间断空格,而不仅仅是最后两个?
针对此修复的 CSS 选择器是否有任何更新,使其成为 100% 的 CSS 解决方案?我已经向 W3c 提交了这个问题很多年了,他们说“啊,这是一个好主意”,但他们并没有将其添加到规范中。
我想会更短一些
return (substr_count($text, ” “) > 1 && substr_count($text, ” “) >= $minWords-1 ? substr_replace(strrpos($text, ” “) , ” “, $lastspace, 1) : $text;
甚至可能是
return (substr_count($text, ” “) > 1 && str_word_count($text) >= $minWords ? substr_replace(strrpos($text, ” “) , ” “, $lastspace, 1) : $text;
(两者都未测试)
抱歉。第二个应该是
return (str_word_count($text) >= $minWords ? substr_replace(strrpos($text, ” “) , “ ”, $lastspace, 1) : $text;
$text = preg_replace( '|([^\s])\s+([^\s]+)\s*$|', '$1 $2', $text);