Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Suma cyfr
lled3
post 22.07.2010, 16:17:50
Post #1





Grupa: Zarejestrowani
Postów: 91
Pomógł: 0
Dołączył: 7.12.2007

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


Jak zsumować cyfry z danej liczby questionmark.gif

np mamy liczbe 2001 = 2+0+0+1=3
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
czachor
post 22.07.2010, 16:23:38
Post #2





Grupa: Zarejestrowani
Postów: 897
Pomógł: 40
Dołączył: 16.12.2003
Skąd: Warszawa

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


  1. $integer = 2001929823;
  2. echo array_sum(str_split($integer));


--------------------
how many SEO experts does it take to change a light bulb,lightbulb,light,bulb,lamp,lighting,switch,sex,xxx
5-Reasons-why-you-should-NEVER-fix-a-computer-for-free
Go to the top of the page
+Quote Post
lled3
post 22.07.2010, 16:29:01
Post #3





Grupa: Zarejestrowani
Postów: 91
Pomógł: 0
Dołączył: 7.12.2007

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


Cytat
Fatal error: Call to undefined function: str_split() in
Go to the top of the page
+Quote Post
exood
post 22.07.2010, 16:32:36
Post #4





Grupa: Zarejestrowani
Postów: 86
Pomógł: 16
Dołączył: 2.12.2009
Skąd: Płock/Warszawa

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


str_split jest dostępne w wersji PHP >= 5
Go to the top of the page
+Quote Post
lled3
post 22.07.2010, 16:35:42
Post #5





Grupa: Zarejestrowani
Postów: 91
Pomógł: 0
Dołączył: 7.12.2007

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


a mam PHP 4 smile.gif i muszę się tym zadowolić.
Go to the top of the page
+Quote Post
evolucja
post 22.07.2010, 16:48:01
Post #6





Grupa: Zarejestrowani
Postów: 362
Pomógł: 27
Dołączył: 14.09.2009

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


To użyj:
  1. function str_split($text, $split = 1)
  2. {
  3. if (!is_string($text)) return false;
  4. if (!is_numeric($split) && $split < 1) return false;
  5. $len = strlen($text);
  6. $array = array();
  7. $s = 0;
  8. $e=$split;
  9. while ($s <$len)
  10. {
  11. $e=($e <$len)?$e:$len;
  12. $array[] = substr($text, $s,$e);
  13. $s = $s+$e;
  14. }
  15. return $array;
  16. }
  17. }


--------------------
zmoderowano - waga i rozmiar
Go to the top of the page
+Quote Post
Crozin
post 22.07.2010, 18:24:44
Post #7





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


Wystarczy mu: explode('', $interger);
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 - 09:13