gevans Posted November 20, 2008 Share Posted November 20, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/133506-solved-csv-string-to-integer/ Share on other sites More sharing options...
mtoynbee Posted November 20, 2008 Share Posted November 20, 2008 $foo = "1,516"; $foo = eregi_replace(",","",$foo); Quote Link to comment https://forums.phpfreaks.com/topic/133506-solved-csv-string-to-integer/#findComment-694395 Share on other sites More sharing options...
gevans Posted November 20, 2008 Author Share Posted November 20, 2008 Does eregi_replace take longer than str_replace? I'm currently using str_replace, just wanted to see if there was anything better. Quote Link to comment https://forums.phpfreaks.com/topic/133506-solved-csv-string-to-integer/#findComment-694402 Share on other sites More sharing options...
mtoynbee Posted November 20, 2008 Share Posted November 20, 2008 I think str_replace might be slightly quicker. ereg_replace is quicker than eregi as it's case insensitive. The difference is minimal though. If you are using str_replace already does that not strip the comma? Quote Link to comment https://forums.phpfreaks.com/topic/133506-solved-csv-string-to-integer/#findComment-694405 Share on other sites More sharing options...
mtoynbee Posted November 20, 2008 Share Posted November 20, 2008 You can just use number_format http://uk.php.net/number_format Quote Link to comment https://forums.phpfreaks.com/topic/133506-solved-csv-string-to-integer/#findComment-694407 Share on other sites More sharing options...
premiso Posted November 20, 2008 Share Posted November 20, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133506-solved-csv-string-to-integer/#findComment-694423 Share on other sites More sharing options...
gevans Posted November 20, 2008 Author Share Posted November 20, 2008 Yes it does, just wanted to see what my other options were. Number Format won't do it, it takes an ineger to be formatted not a string to reformat as an integer. Quote Link to comment https://forums.phpfreaks.com/topic/133506-solved-csv-string-to-integer/#findComment-694425 Share on other sites More sharing options...
premiso Posted November 20, 2008 Share Posted November 20, 2008 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 ?> =) Quote Link to comment https://forums.phpfreaks.com/topic/133506-solved-csv-string-to-integer/#findComment-694428 Share on other sites More sharing options...
gevans Posted November 20, 2008 Author Share Posted November 20, 2008 Haha, yes that is slightly overkill!! Think I got it right the first time, I do love trying to run things faster though so always worth an ask! Quote Link to comment https://forums.phpfreaks.com/topic/133506-solved-csv-string-to-integer/#findComment-694437 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.