Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]przycinanie tekstu, bez naruszania wyrazów
barthpl
post
Post #1





Grupa: Zarejestrowani
Postów: 142
Pomógł: 35
Dołączył: 5.12.2006
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


Mamy taki oto skrypcik, który przycina tekst do określanej długości nie przerywając wyrazów. Nie wydaje mi się on optymalny dlatego czy może ma ktoś pomysł na jego naprawę.
  1. <?php
  2. /**
  3.  * This function returns substring no longer then indicated length but without cutting words apart.
  4.  * @param string - input string;
  5.  * @param int - max length of the output string;
  6.  * @param string - string attached to the output;
  7.  * @return string
  8.  *
  9.  */
  10. function trimString( $string, $length, $suffix )
  11. {
  12.    if( ($strlen = strlen($string)) < $length )
  13.        return $string.($strlen > 0 ? $suffix : null);
  14.  
  15.    $hash = md5( $string .' '. $length .' '. $suffix );
  16.    $break = "<![CDATA[$hash]]>";
  17.    $strtmp = wordwrap( $string, $length, $break, false );
  18.    $strarr = explode( $break, $strtmp, 2);
  19.    return $strarr[0].$suffix;
  20. }
  21. ?>


Jakieś pomysły (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 4.10.2025 - 17:45