Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]CURL odczyt wartości i zamiana na liczbę jak ?, chce zsumować dwie zmienne.
casperii
post
Post #1





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


Panowie pobieram poprzez curla dwie zmienne:
$koszt = 100.00;
$dostawa = 20.00;
$lacznie = $cena + $dostawa;

  1. if (strpos($n->innertext, "zł") !== FALSE) {
  2. $transport = $n->innertext;
  3. $transport = str_replace('Koszt:' ,'', $transport);
  4. $transport = trim(str_replace('zł', '', $transport));
  5. $transport = str_replace(',' ,'.', $transport);
  6. }


niestety $koszt i $dostawa są prawdopodobnie traktowane jako string, jak zamienić na typ liczbowy bym mógł zsumować te 2 zmienne?
próbowałem int(), number_format(), float().

Ma ktoś jakiś pomysł ?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 13)
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Moze pokaz co dokladnie zawiera ten tekst a nie cudujesz na slepo
var_dump
Go to the top of the page
+Quote Post
Neutral
post
Post #3





Grupa: Zarejestrowani
Postów: 286
Pomógł: 46
Dołączył: 10.01.2016

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


Użyj var_dump i sprawdź jaki to ma typ, a co do rzutowania, to chyba tak można, by napisać:

  1. $var= 'string1';
  2. $var = (int) $var;
  3. var_dump($var);


http://php.net/manual/en/ref.var.php

Edit: Nospor mnie ubiegł.

Ten post edytował Neutral 6.12.2017, 18:13:56
Go to the top of the page
+Quote Post
casperii
post
Post #4





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


var_dump($var);
int(0)

var_dump($transport);
string(80)

Ten post edytował casperii 6.12.2017, 18:21:25
Go to the top of the page
+Quote Post
Neutral
post
Post #5





Grupa: Zarejestrowani
Postów: 286
Pomógł: 46
Dołączył: 10.01.2016

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


Jeśli w tym osiemdziesięcio-znakowym string'u są białe znaki to może użyj funkcji trim() lub str_replace()?

http://php.net/manual/pl/function.str-replace.php

Napisz też, czy po zrzutowaniu masz int'a, czy nie.

Ten post edytował Neutral 6.12.2017, 18:38:24
Go to the top of the page
+Quote Post
casperii
post
Post #6





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


  1. if (strpos($n->innertext, "zł") !== FALSE) {
  2. $transport = $n->innertext;
  3. $transport = str_replace('Koszt:' ,'', $transport);
  4. $transport = trim(str_replace('zł', '', $transport));
  5. $transport = str_replace(',' ,'.', $transport);
  6. $transport = trim($transport);
  7. }
  8. $var = (int)$transport;
  9. $var2 = $transport;
  10. var_dump($var);
  11. var_dump($var2)


@nospor jakiś pomysł ?(IMG:style_emoticons/default/smile.gif)
int(0)
string(80) " 283.00 "

Ten post edytował casperii 6.12.2017, 19:04:22
Go to the top of the page
+Quote Post
trzczy
post
Post #7





Grupa: Zarejestrowani
Postów: 460
Pomógł: 49
Dołączył: 5.06.2011

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


Zmiana na liczbę
  1. filter_var($stringFloat, FILTER_VALIDATE_FLOAT);
Go to the top of the page
+Quote Post
casperii
post
Post #8





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


@trzczy twój kod daje:
bool(false)
Go to the top of the page
+Quote Post
trzczy
post
Post #9





Grupa: Zarejestrowani
Postów: 460
Pomógł: 49
Dołączył: 5.06.2011

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


A co zwraca to
  1. var_dump($n->innertext);

Bo $n->innertext jest chyba tekstem wyjściowym.

Ten post edytował trzczy 6.12.2017, 20:06:45
Go to the top of the page
+Quote Post
casperii
post
Post #10





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


zwraca:
string(133) " Koszt: 283,00 zł "

ale powyższe parsuje
  1. $transport = str_replace('Koszt:' ,'', $transport); usuwam koszt:
  2. $transport = str_replace('zł', '', $transport); //usuwam zł
  3. $transport = preg_replace('/\s+/', '', $transport); //usuwam niewidoczne znaki "spacje"
  4. $transport = str_replace(',' ,'.', $transport); //zamieniam przecinek na kropke

Go to the top of the page
+Quote Post
trzczy
post
Post #11





Grupa: Zarejestrowani
Postów: 460
Pomógł: 49
Dołączył: 5.06.2011

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


A tutaj działa:
https://3v4l.org/GIo16
Go to the top of the page
+Quote Post
casperii
post
Post #12





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


no widzisz tam działa, a u mnie nie.
Jak na "sztywno" wpisałem sobie
$transport = "283" to twój kod działa, dziwne..

Ten post edytował casperii 6.12.2017, 20:44:43
Go to the top of the page
+Quote Post
trzczy
post
Post #13





Grupa: Zarejestrowani
Postów: 460
Pomógł: 49
Dołączył: 5.06.2011

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


Spróbuj sam
bo wtedy bierzesz tylko to, co cię interesuje demo

Ten post edytował trzczy 6.12.2017, 21:46:13
Go to the top of the page
+Quote Post
casperii
post
Post #14





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

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


preg_match rozwiązał problem. dziękuje.
Go to the top of the page
+Quote Post

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: 16.09.2025 - 08:48