Jump to content

[SOLVED] CSV string to integer


gevans

Recommended Posts

Hey guys,

 

I'm currently working on a csv importer.

 

The importer works fine, and one of the pieces of data is a number, currently it's 1,516 (it can be anything from 0 up)

 

I need to save this number as an integer, as you'd imagine setting it to an integer loses the numbers after the thousands' comma;

 

$foo = "1,516";
echo (int)$foo;// outputs 1

 

What's the best way to strip the comma out?

Link to comment
https://forums.phpfreaks.com/topic/133506-solved-csv-string-to-integer/
Share on other sites

Looks like str_replace would be the way to go or ereg_replace (gathered this from reading the comments on int type-casting). Either will work.

 

EDIT:

Source for more information

http://us.php.net/manual/en/function.intval.php#76803

 

Also removed number_format after reading it does not take strings for a param.

Yes it does, just wanted to see what my other options were. Number Format won't do it, it takes an integer to be formatted not a string to reformat as an integer.

 

After reading the comments it seems only the replace functions are your options...well other than using explode then implode which would be silly to do, but an example of that is:

 

<?php
$string = "123,423,234";
$int = (int) implode("", explode(",", $string));

echo $int; // should output 123423234
?>

 

=)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.