smerny Posted April 18, 2010 Share Posted April 18, 2010 I did a bit of searching and I can't find anything that will do strings to ints effectively... by this I mean convert "1,423,183" to 1423183 (not 1) convert "16.2k" to 16200 (not 16) does anyone know of any? Link to comment https://forums.phpfreaks.com/topic/198957-strings-to-ints/ Share on other sites More sharing options...
litebearer Posted April 18, 2010 Share Posted April 18, 2010 Are you dealing with anything other than commas, decimals and 'k'? Link to comment https://forums.phpfreaks.com/topic/198957-strings-to-ints/#findComment-1044346 Share on other sites More sharing options...
smerny Posted April 18, 2010 Author Share Posted April 18, 2010 actually never any decimals, always ints Link to comment https://forums.phpfreaks.com/topic/198957-strings-to-ints/#findComment-1044348 Share on other sites More sharing options...
litebearer Posted April 18, 2010 Share Posted April 18, 2010 I meant in the string Link to comment https://forums.phpfreaks.com/topic/198957-strings-to-ints/#findComment-1044350 Share on other sites More sharing options...
smerny Posted April 18, 2010 Author Share Posted April 18, 2010 never will show any decimals (like 12.5) but will show like the example, an integer shown in a notation where there is a decimal point and a k (like 12.5k) Link to comment https://forums.phpfreaks.com/topic/198957-strings-to-ints/#findComment-1044357 Share on other sites More sharing options...
litebearer Posted April 18, 2010 Share Posted April 18, 2010 Psuedo code... function my_int($string) { /* check to see if 'k' is present' */ if(substr($string, -1)== 'k') { /* remove 'k' from the string */ $string = substr_replace($string ,"",-1); /* remove any commas from the string */ $string = str_replace(",","",$string); /* create integer */ $new_int = $string * 1000; return $new_int; } $string = str_replace(",","",$string); $new_int = $string * 1; return $new_int; } untested - old man's mind wandering Link to comment https://forums.phpfreaks.com/topic/198957-strings-to-ints/#findComment-1044364 Share on other sites More sharing options...
smerny Posted April 19, 2010 Author Share Posted April 19, 2010 It works, I changed it a bit function myStr2Int($str) { $str = str_replace(",","",$str); if (substr($str, -1)=="k") { $str = str_replace("k","",$str) * 1000; } return (int) $str; } thanks Link to comment https://forums.phpfreaks.com/topic/198957-strings-to-ints/#findComment-1044369 Share on other sites More sharing options...
andrewgauger Posted April 19, 2010 Share Posted April 19, 2010 //Thank you [email protected] --shameless endorsement function intFromStr($strVal){ $lastChar=substr($strVal, -1, 1); $allbutLast=substr($strVal, 0, -1); if (preg_match('/[0-9]/',$lastChar)) { $strVal=preg_replace('/[,]/', '', $strVal); return (int)$strVal; } else //if the last character is a letter do one: { switch ($lastChar){ case "k": case "K": return (int)(((float) $allbutLast)*1000); case "m": case "M": return (int)(((float) $allbutLast)*1000000); default: return FALSE; } } } Link to comment https://forums.phpfreaks.com/topic/198957-strings-to-ints/#findComment-1044370 Share on other sites More sharing options...
smerny Posted April 19, 2010 Author Share Posted April 19, 2010 dont need the flexibility of that yet and don't think i will, but i suppose it couldn't hurt did this: function myStr2Int($str) { $str = str_replace(",","",$str); if (in_array(substr($str, -1),array("k","K"))) $str = $str * 1000; elseif (in_array(substr($str, -1),array("m","M"))) $str = $str * 1000000; return (int) $str; } Link to comment https://forums.phpfreaks.com/topic/198957-strings-to-ints/#findComment-1044378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.