Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

2 Stron V   1 2 >  
Reply to this topicStart new topic
> [PHP] jak zmienić formę wyświetlania stringu, operacje na stringach
-cobol-
post
Post #1





Goście







mam stringi np:
  1. <?php
  2. $a = "9";
  3. $b = "1235";
  4. $c = "123";
  5. $d= "12";
  6. ?>

i jak je przekształcić, aby było zachowane formatowanie xx:xx
np:
  1. <?php
  2. $a = "00:09";
  3. $b = "12:35";
  4. $c = "01:23";
  5. $d= "00:12";
  6. ?>

?
Go to the top of the page
+Quote Post
bim2
post
Post #2





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

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


  1. <?php
  2. $tmp = floor($a/60);
  3. $hour = $tmp;
  4. $minutes = $a-($tmp*60);
  5. if(strlen($hour)==1)
  6. {
  7.  $hour = '0'.$hour;
  8. }
  9. if(strlen($minutes)==1)
  10. {
  11.  $minutes = '0'.$minutes;
  12. }
  13.  
  14. echo $hour.':'.$minutes;
  15. ?>


Ten post edytował bim2 15.11.2008, 15:43:09
Go to the top of the page
+Quote Post
-cobol-
post
Post #3





Goście







dziękuję bim2


// $pomógł++; (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
// ~ayeo
Go to the top of the page
+Quote Post
-cobol-
post
Post #4





Goście







ale przez to zaokrąglenie źle pokazuje (IMG:http://forum.php.pl/style_emoticons/default/sad.gif)
Go to the top of the page
+Quote Post
bim2
post
Post #5





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

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


jak źle pokazuje? :]
Go to the top of the page
+Quote Post
-cobol-
post
Post #6





Goście







np dla $a = "123";
pokazuje 02:03 zamiast 01:23
Go to the top of the page
+Quote Post
ayeo
post
Post #7





Grupa: Przyjaciele php.pl
Postów: 1 202
Pomógł: 117
Dołączył: 13.04.2007
Skąd: 127.0.0.1

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


Witam!

123 sekundy to chyba 2 minuty i 3 sekundy? Czyli pokazuje dobrze (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
Jak chcesz dodać po prostu dwukropek co dwie cyfry to RTFM

Pozdrawiam!
Go to the top of the page
+Quote Post
bim2
post
Post #8





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

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


o boze ... Było mówić, że 123 to nie są minuty. (W sumie moja wina że nie spostrzegłem (IMG:http://forum.php.pl/style_emoticons/default/haha.gif) )
  1. <?php
  2. $len = strlen($a);
  3. $new = '';
  4. for($i=0;$i<$len;$i++)
  5. {
  6. $new .= $a{$i};
  7. if($i==1)
  8. {
  9. $new .= ':';
  10. }
  11. }
  12. echo $new;
  13. ?>


Ten post edytował bim2 15.11.2008, 16:29:05
Go to the top of the page
+Quote Post
-cobol-
post
Post #9





Goście







teraz jest ok dla czterocyfrowych stringów, ale jeśli
  1. <?php
  2. $a = "3";
  3. $b = "33";
  4. $c = "133";
  5. ?>

to już nie jest ok ;(
powinno uzupełniać zerami do 4 cyfr od początku i wtedy po 2 cyfrze dodać dwukropek.
efekt końcowy:
  1. <?php
  2. $a = "00:03";
  3. $b = "00:33";
  4. $c = "01:33";
  5. ?>


ps wole cobol i jcl , w php się gubię (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
Go to the top of the page
+Quote Post
bim2
post
Post #10





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

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


  1. <?php
  2. $a = '133';
  3. if(strlen($a)<2)
  4. {
  5.    $a = '00:0'.$a;
  6. }
  7. elseif(strlen($a)<3)
  8. {
  9.    $a = '00:'.$a;
  10. }
  11. elseif(strlen($a)<4)
  12. {
  13.    $a = '0'.$a{0}.':'.$a{1}.$a{2};
  14. }
  15. elseif(strlen($a)<5)
  16. {
  17.    $a = $a{0}.$a{1}.':'.$a{2}.$a{3};
  18. }
  19.  
  20. echo $a;
  21. ?>

Sam sprawdziłem. Już działa.
Go to the top of the page
+Quote Post
ayeo
post
Post #11





Grupa: Przyjaciele php.pl
Postów: 1 202
Pomógł: 117
Dołączył: 13.04.2007
Skąd: 127.0.0.1

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


  1. <?php
  2. $string = "123";
  3. $pattern = "#([0-9]{2})$#";
  4. $string = preg_replace($pattern, ":$1", $string);
  5. echo $string;
  6. ?>


Pozdrawiam!
Go to the top of the page
+Quote Post
bim2
post
Post #12





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

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


~ayeo może i prościej, ale dłużej działa (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
-cobol-
post
Post #13





Goście







Cytat(bim2 @ 15.11.2008, 16:39:19 ) *
Sam sprawdziłem. Już działa.

teraz jest super (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

$dzięki++;
Go to the top of the page
+Quote Post
ayeo
post
Post #14





Grupa: Przyjaciele php.pl
Postów: 1 202
Pomógł: 117
Dołączył: 13.04.2007
Skąd: 127.0.0.1

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


@bim2, hehe. Najpardziej przekombinowane rozwiązanie banalnego problemu:
  1. <?php
  2. $string = "123";
  3. $pattern = "#([0-9]{2})$#";
  4.  
  5. if(preg_match($pattern, $string))
  6. {
  7.    $string = preg_replace($pattern, ":$1", $string);
  8. }
  9. else
  10. {
  11.    $string = strlen($string) == 1 ? "0".$string : $string;
  12.    $string = "00:".$string;
  13. }
  14. echo $string;
  15. ?>


Pozdrawiam!
Go to the top of the page
+Quote Post
bim2
post
Post #15





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

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


Wiesz co, nie zgodzę się. (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) Mój sposób działa najszybciej (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) Moim zdaniem twoje jest przekombinowane. Po co używać dla max 4 literowego stringa wyrażeń regularnych.
Go to the top of the page
+Quote Post
erix
post
Post #16





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




(nie) Macie obaj racji. (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)

~ayeo by wciskał pregi, gdzie tylko się da, za to Ty robisz kilka razy strlen" title="Zobacz w manualu PHP" target="_manual. (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post
bim2
post
Post #17





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

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


Czepialski (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif) :D

  1. <?php
  2. $a = '133';
  3. $len = strlen($a);
  4. if($len<2)
  5. {
  6.   $a = '00:0'.$a;
  7. }
  8. elseif($len<3)
  9. {
  10.   $a = '00:'.$a;
  11. }
  12. elseif($len<4)
  13. {
  14.   $a = '0'.$a{0}.':'.$a{1}.$a{2};
  15. }
  16. elseif($len<5)
  17. {
  18.   $a = $a{0}.$a{1}.':'.$a{2}.$a{3};
  19. }
  20.  
  21. echo $a;
  22. ?>
Go to the top of the page
+Quote Post
ayeo
post
Post #18





Grupa: Przyjaciele php.pl
Postów: 1 202
Pomógł: 117
Dołączył: 13.04.2007
Skąd: 127.0.0.1

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


@bim2 - przekombinowane napisałem o swoim kodzie (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

  1. <?php
  2. $string = "253";
  3. while( strlen( $string ) < 4) $string = "0".$string;
  4. $string = preg_replace("#([0-9]{2})$#", ":$1",  $string );
  5. echo $string;
  6. ?>


Da rade lepiej?
Go to the top of the page
+Quote Post
erix
post
Post #19





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




Cytat

Skoro mówiliśmy o szybkości działania, to trzeba było się czepić. (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)

Kod
0.042402982711792
0.23657393455505


Tysiąc iteracji, kod ~bim2 jest szybszy. (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post
melkorm
post
Post #20





Grupa: Zarejestrowani
Postów: 1 366
Pomógł: 261
Dołączył: 23.09.2008
Skąd: Bydgoszcz

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


  1. <?php
  2. $a = 123;
  3. $temp = sprintf("%04d",$a);
  4. echo substr($temp,0,2).' : '.substr($temp,2,4);
  5. ?>

jakoś tak .... naturalniej ;]

albo :

  1. <?php
  2. $temp = sprintf("%04d",$a);
  3. echo $temp[0].$temp[1].' : '.$temp[2].$temp[3];
  4. ?>

(IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)

Ten post edytował melkorm 15.11.2008, 18:16:33
Go to the top of the page
+Quote Post

2 Stron V   1 2 >
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: 23.12.2025 - 01:10