Convert php to int int format

Sorry for the question, but I can not find a solution that will get this - from 7000 to 7000.

I use intval () and number_format (), but it just gives me 7 not 7000.

$myVal = '7,000'; echo intval($myVal); 

it returns 7

the same thing happens with number_format ()

what am I doing wrong?

+6
source share
2 answers

intval() will not work with your string because of a comma. You can remove the comma using str_replace() and then calling intval() as follows:

echo intval(str_replace(',', '', $myVal))

+6
source

If you use PHP> = 5.3 (or have the "intl" extension installed at least with version 1.0), you can also use the NumberFormatter class to parse the local value.

 $myVal = '7,000'; $nf = new NumberFormatter("en_EN", NumberFormatter::DECIMAL); var_dump($nf->parse($myVal, NumberFormatter::TYPE_INT32)); # output: int(7000) 

If you are using a PHP version less than 5.3, you are better off using @KevinS. Decision.

+10
source

Source: https://habr.com/ru/post/918057/


All Articles