[eluser]Chicken's Egg[/eluser]
Quote:Will it be ok if I use your _let_to_num($v) in the new version of the code?
Of course, but you might be even more interested in this one:
Code:
function let_to_bit($sValue)
{
// Split value from name
if(!preg_match('/([0-9]+)([ptgmkb]{1,2}|)/ui',$sValue,$aMatches))
{ // Invalid input
return FALSE;
};
if(empty($aMatches[2]))
{ // No name -> Enter default value
$aMatches[2] = 'KB';
};
if(strlen($aMatches[2]) == 1)
{ // Shorted name -> full name
$aMatches[2] .= 'B';
}
$iBit = (substr($aMatches[2], -1) == 'B') ? 1024 : 1000;
// Calculate bits:
switch(strtoupper(substr($aMatches[2],0,1))){
case 'P':
$aMatches[1] *= $iBit;
case 'T':
$aMatches[1] *= $iBit;
case 'G':
$aMatches[1] *= $iBit;
case 'M':
$aMatches[1] *= $iBit;
case 'K':
$aMatches[1] *= $iBit;
break;
}
// Return the value in bits
return $aMatches[1];
}
// The following lines were used to test the above function:
echo '1 '.let_to_bit('') . '<br />'; // returns FALSE
echo '2 '.let_to_bit('2') . '<br />'; // returns 2048
echo '3 '.let_to_bit('2K') . '<br />'; // returns 2048
echo '4 '.let_to_bit('2L') . '<br />'; // returns 2048
echo '5 '.let_to_bit('2Kb') . '<br />'; // returns 2000
echo '6 '.let_to_bit('2KB') . '<br />'; // returns 2048
I wrote the function for your extension, so please feel free to use it if you like.