Jump to content

Strings to Ints


smerny

Recommended Posts

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

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

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

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.