S
S
Sergey2014-09-24 13:08:09
PHP
Sergey, 2014-09-24 13:08:09

How can I convert a string to a number?

Saved excel to csv on mac os. As a result, a file came out in windows-1251 encoding like (in Notepad ++ it is written ANSI). This file has a cell with a price of "2,568".
The problem is that I can't convert it to integer.
$price = '2568';
intval(price) //int(2)
str_replace(' ', '', $price) //string(6) "2 568"
preg_replace('/\s+/', '', $price) //string( 6) "2 568"
iconv("windows-1251", "utf-8", $price)//string(8) "2В 568"
How to get the value 2568 correctly?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Burov, 2014-09-24
@frost18

$price = (int)preg_replace('/[^0-9]/','',$price);it?

E
Evgeniy Samoilenko, 2014-09-24
@samoilenkoevgeniy

1) remove spaces from number
2) either explicit conversion (int) or $string = 'xyz22' + 0 // 22

S
Sergey, 2014-09-24
@frost18

$price = (int)preg_replace('/[^0-9]/','',$price);
it worked, thanks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question