Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] funkcja
Forum PHP.pl > Forum > PHP
mathevs
  1. function limit_chars($str, $limit = 100, $end_char = NULL, $preserve_words = FALSE)
  2.  
  3. {
  4.  
  5. $end_char = ($end_char === NULL) ? '?' : $end_char;
  6.  
  7.  
  8.  
  9. $limit = (int) $limit;
  10.  
  11.  
  12.  
  13. if (trim($str) === '' OR strlen($str) <= $limit)
  14.  
  15. return $str;
  16.  
  17.  
  18.  
  19. if ($limit <= 0)
  20.  
  21. return $end_char;
  22.  
  23.  
  24.  
  25. if ($preserve_words === FALSE)
  26.  
  27. return rtrim(substr($str, 0, $limit)).$end_char;
  28.  
  29.  
  30.  
  31. // Don't preserve words. The limit is considered the top limit.
  32.  
  33. // No strings with a length longer than $limit should be returned.
  34.  
  35. if ( ! preg_match('/^.{0,'.$limit.'}\s/us', $str, $matches))
  36.  
  37. return $end_char;
  38.  
  39.  
  40.  
  41. return rtrim($matches[0]).((strlen($matches[0]) === strlen($str)) ? '' : $end_char);
  42.  
  43. }


teraz niech ktoś mi powie jak to podlączyć ... próbowałem ale coś mi nie wychodzi haha.gif

z góry dzięki tongue.gif

muniekw
A w jaki sposób próbowałeś to podczepiać?
mathevs
  1. function limit_chars($str) {
  2.  
  3. $limit = 18;
  4.  
  5. $end_char = NULL;
  6.  
  7. $preserve_words = FALSE;
  8.  
  9. $end_char = ($end_char === NULL) ? '?' : $end_char;
  10. $limit = (int) $limit;
  11.  
  12.  
  13. if (trim($str) === '' OR strlen($str) <= $limit)
  14.  
  15. return $str;
  16.  
  17.  
  18. if ($limit <= 0)
  19.  
  20. return $end_char;
  21.  
  22.  
  23. if ($preserve_words === FALSE)
  24.  
  25. return rtrim(substr($str, 0, $limit)).$end_char;
  26.  
  27.  
  28. if ( ! preg_match('/^.{0,'.$limit.'}\s/us', $str, $matches)) {
  29.  
  30. return $end_char;
  31.  
  32.  
  33.  
  34. return rtrim($matches[0]).((strlen($matches[0]) === strlen($str)) ? '' : $end_char);
  35.  
  36. }


dodałem to do skryptu funkcje.php ...
i w index.php zrobiłem tak


  1.  
  2. $xDDDDD = 'siemanko z tej strony xxx';
  3.  
  4. limit_chars($xDDDDD);
  5.  
CuteOne
Jakieś błędy? w ogóle echowałeś wynik funkcji ( echo limit_chars($xDDDDD); )?
mathevs
już wiem haha.gif zrobiłem dobrze tylko zapomniałerm o echo .. haha.gif kolega przypomniał tongue.gif
phpion
Cieszę się razem z Tobą, ale na przyszłość proszę pamiętaj sensowniej tytułować wątki. "funkcja" niewiele mówi...
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.