<?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;
}
?>
Sie können dies kürzen auf
= $minWords)
? ' ' . array_pop($output)
: ' ' . array_pop($output);
$output = implode(' ',$output) . $widow;
return $output;
}
?>
GENAU das, was ich brauchte. Danke!
Wie fügt man nicht umbrechende Leerzeichen zu allen Wörtern mit weniger als 5 Buchstaben hinzu, nicht nur zu den letzten 2?
Gibt es ein Update der CSS-Selektoren, damit diese Korrektur eine 100%ige CSS-Lösung ist? Ich plage W3c schon seit Ewigkeiten damit – „Ah ja, das ist eine gute Idee“ – und dann fügen sie es nicht in die Spezifikation auf.
Wäre wohl kürzer
return (substr_count($text, ” “) > 1 && substr_count($text, ” “) >= $minWords-1 ? substr_replace(strrpos($text, ” “) , ” “, $lastspace, 1) : $text;
Vielleicht sogar
return (substr_count($text, ” “) > 1 && str_word_count($text) >= $minWords ? substr_replace(strrpos($text, ” “) , ” “, $lastspace, 1) : $text;
(beide ungetestet)
Entschuldigung. Das zweite sollte sein
return (str_word_count($text) >= $minWords ? substr_replace(strrpos($text, ” “) , “ ”, $lastspace, 1) : $text;
$text = preg_replace( '|([^\s])\s+([^\s]+)\s*$|', '$1 $2', $text);