Mailinglisten-Archive |
Michael Borchers schrieb:
>> Michael Borchers schrieb:
>>> prinz. kann ich ja mit substr auf X zeichen limitieren a la
>>> substr($str, 0, 10);
>>>
>>> dabei werden ganze wörter ja manchmal mit abgeschnitten. gibt es eine
>>> option
>>> "bis zum zeichen X, aber das letzte wort nicht abschneiden, sondern noch
>>> ausschreiben?!"
>> nein - aber wordwrap() hilft dir da
>
> Ein paar Zeilen tiefer half mir folgende Funktion!
>
> lorang at e-connect dot lu
>
> Hello,
> If you nee a function that limits a text to a certain number of characters,
> but it should always keep the whole words, try this :
>
> function limit_text($text,$maxchar){
> $split=explode(" ",$text);
> $i=0;
> while(TRUE){
> $len=(strlen($newtext)+strlen($split[$i]));
> if($len>$maxchar){
> break;
> }else{
> $newtext=$newtext." ".$split[$i];
> $i++;
> }
> }
> return $newtext;
> }
sieht aber umständlich aus (und wirft mindestens ein NOTICE) ...
function strWordCut($text, $max_len = 76)
{
$newtext = '';
foreach (explode(" ", $text) as $word) {
if (strlen($newtext . ' ' . $word) > $max_len) {
return $newtext;
}
$newtext .= ' ' . $word;
}
return $newtext;
}
--
Sebastian Mendel
www.sebastianmendel.de
php::bar PHP Wiki - Listenarchive