Jump to content

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
?>

 

=)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.