Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] rozwiązany -> obcięcie tekstu z bazy ale nie ucięcie w połowie słowa
john_doe
post
Post #1





Grupa: Zarejestrowani
Postów: 873
Pomógł: 25
Dołączył: 24.07.2005

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


Witam,

mam pole w bazie zawierające text. Text oczywiście a różną długość. Zrobiłem wyświetlanie wpisów i substring( text,0,100) i link czytaj dalej. Jednak wiadomo, że taki sposób jest do luftu bo ucina wyrazy etc.
Jak zrobić aby zostawić pełny wyraz na końcu?

pozdrawiam

EDIT:

w oparciu o google napisałem helper w CI

  1. <?php
  2.  
  3. /**
  4.  * truncate_string
  5.  *
  6.  * function trims a text without breaking the word just in the middle
  7.  * The simple algorithm is as follows:
  8.  * - Is the string longer than our limit ( in this case 250 )
  9.  * - Truncate the string to our limit
  10.  * - If we ended up in the middle of a word
  11.  * - Trim out that broken word by finding the previous space
  12.  *
  13.  * @access public
  14.  * @param $str given string to trim
  15.  * @param $max_len max length to remain after trim
  16.  * @return $new_str trimed string
  17.  */
  18. if ( ! function_exists('truncate_string'))
  19. {
  20. function truncate_string($str, $max_len)
  21. {
  22. if ( strlen($str) <= $max_len )
  23. return $str;
  24.  
  25. $new_str = substr($str, 0, $max_len);
  26. if ( substr($new_str,-1,1) != ' ' ) $new_str = substr($new_str, 0, strrpos($new_str, " "));
  27.  
  28. return $new_str;
  29. }
  30. }
  31.  
  32. ?>


Ten post edytował john_doe 17.08.2011, 23:04:28
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 20.08.2025 - 10:07