Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] funkcja
mathevs
post 3.01.2013, 15:37:20
Post #1





Grupa: Zarejestrowani
Postów: 39
Pomógł: 0
Dołączył: 5.11.2012

Ostrzeżenie: (10%)
X----


  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

Go to the top of the page
+Quote Post
muniekw
post 3.01.2013, 15:40:34
Post #2





Grupa: Zarejestrowani
Postów: 243
Pomógł: 22
Dołączył: 1.06.2009
Skąd: Warszawa

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


A w jaki sposób próbowałeś to podczepiać?
Go to the top of the page
+Quote Post
mathevs
post 3.01.2013, 15:42:32
Post #3





Grupa: Zarejestrowani
Postów: 39
Pomógł: 0
Dołączył: 5.11.2012

Ostrzeżenie: (10%)
X----


  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.  


Ten post edytował mathevs 3.01.2013, 15:42:48
Go to the top of the page
+Quote Post
CuteOne
post 3.01.2013, 15:48:30
Post #4





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

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


Jakieś błędy? w ogóle echowałeś wynik funkcji ( echo limit_chars($xDDDDD); )?

Ten post edytował CuteOne 3.01.2013, 15:48:44
Go to the top of the page
+Quote Post
mathevs
post 3.01.2013, 16:05:39
Post #5





Grupa: Zarejestrowani
Postów: 39
Pomógł: 0
Dołączył: 5.11.2012

Ostrzeżenie: (10%)
X----


już wiem haha.gif zrobiłem dobrze tylko zapomniałerm o echo .. haha.gif kolega przypomniał tongue.gif
Go to the top of the page
+Quote Post
phpion
post 3.01.2013, 19:46:04
Post #6





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Cieszę się razem z Tobą, ale na przyszłość proszę pamiętaj sensowniej tytułować wątki. "funkcja" niewiele mówi...
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 19.07.2025 - 00:10