Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] wyświetlenie x znaków
GreenGo
post
Post #1





Grupa: Zarejestrowani
Postów: 265
Pomógł: 4
Dołączył: 30.08.2004

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


Witam.
Mam tekst w bazie o dłógości np. 500 znaków.
W jaki sposób moge wyświetlić piewsze 100 znaków tego tekstu ?
Pozdrawiam.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
mike
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

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


substr()
lub ładniej (rodem ze Smarty'ego):
  1. <?php
  2. /**
  3.  * Purpose: Truncate a string to a certain length if necessary,
  4.  *  optionally splitting in the middle of a word, and
  5.  *  appending the $etc string.
  6.  * @param string
  7.  * @param integer
  8.  * @param string
  9.  * @param boolean
  10.  * @return string
  11.  */
  12. function truncate($string, $length = 80, $etc = '...', $break_words = false)
  13. {
  14. if ($length == 0)
  15. return '';
  16.  
  17. if (strlen($string) > $length) {
  18. $length -= strlen($etc);
  19. if (!$break_words)
  20. $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
  21.  
  22. return substr($string, 0, $length).$etc;
  23. } else
  24. return $string;
  25. }
  26.  
  27. ?>


---update---
~OnE.Killer był szybciej (IMG:http://forum.php.pl/style_emoticons/default/smile.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 - 13:29