Is there an easy way in PHP to convert from strings like '256M', '180K', '4G' to their integer equivalents?
<p>I need to test the value returned by <code>ini_get('memory_limit')</code> and increase the memory limit if it is below a certain threshold, however this <code>ini_get('memory_limit')</code> call returns string values like '128M' rather than integers.</p>

<p>I know I can write a function to parse these strings (taking case and trailing 'B's into account) as I have written them numerous times:</p>

<pre><code>function int_from_bytestring ($byteString) {
  preg_match('/^\s*([0-9.]+)\s*([KMGTPE])B?\s*$/i', $byteString, $matches);
  $num = (float)$matches[1];
  switch (strtoupper($matches[2])) {
    case 'E':
      $num = $num * 1024;
    case 'P':
      $num = $num * 1024;
    case 'T':
      $num = $num * 1024;
    case 'G':
      $num = $num * 1024;
    case 'M':
      $num = $num * 1024;
    case 'K':
      $num = $num * 1024;
  }

  return intval($num);
}
</code></pre>

<p>However, this gets tedious and this seems like one of those random things that would already exist in PHP, though I've never found it. Does anyone know of some built-in way to parse these byte-amount strings?</p>
