Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] po 18 literach lub liczbach 3 kropki ...
Forum PHP.pl > Forum > PHP
mathevs
Witam !

Jak w php napisać cos takiego ..
News wyswietlany z bazy

Witam wszystkich na forum php <---temat

a jak zrobic coś takiego np że po 18 literach lub liczbach wyswietla tytuł 18 literowy i po tych 18 literach dodają sie 3 kropki

Witam wszystkich ...
Czytaj dalej ...

nie umiem tego wytłumaczyc ale mniejsza ... mam nadzieje że zrozumieliście o co chodzi
mokry
Kod, który ucina string'a po x słowach:
  1. function limit_words($str, $limit = 100, $end_char = NULL)
  2. {
  3. $limit = (int) $limit;
  4. $end_char = ($end_char === NULL) ? '?' : $end_char;
  5.  
  6. if (trim($str) === '')
  7. return $str;
  8.  
  9. if ($limit <= 0)
  10. return $end_char;
  11.  
  12. preg_match('/^\s*+(?:\S++\s*+){1,'.$limit.'}/u', $str, $matches);
  13.  
  14. // Only attach the end character if the matched string is shorter
  15. // than the starting string.
  16. return rtrim($matches[0]).((strlen($matches[0]) === strlen($str)) ? '' : $end_char);
  17. }


Funkcja, która ucina string'a po x literach:
  1. function limit_chars($str, $limit = 100, $end_char = NULL, $preserve_words = FALSE)
  2. {
  3. $end_char = ($end_char === NULL) ? '?' : $end_char;
  4.  
  5. $limit = (int) $limit;
  6.  
  7. if (trim($str) === '' OR strlen($str) <= $limit)
  8. return $str;
  9.  
  10. if ($limit <= 0)
  11. return $end_char;
  12.  
  13. if ($preserve_words === FALSE)
  14. return rtrim(substr($str, 0, $limit)).$end_char;
  15.  
  16. // Don't preserve words. The limit is considered the top limit.
  17. // No strings with a length longer than $limit should be returned.
  18. if ( ! preg_match('/^.{0,'.$limit.'}\s/us', $str, $matches))
  19. return $end_char;
  20.  
  21. return rtrim($matches[0]).((strlen($matches[0]) === strlen($str)) ? '' : $end_char);
  22. }


--
Edit: Mała poprawka drugiej funkcji.
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.