Here is another way to get the result in bytes using PHP8
<?php
public function getBytes(string $size): int
{
$size = trim($size);
preg_match('/([0-9]+)[\s]*([a-zA-Z]+)/', $size, $matches);
$value = (isset($matches[1])) ? $matches[1] : 0;
$metric = (isset($matches[2])) ? strtolower($matches[2]) : 'b';
$value *= match ($metric) {
'k', 'kb' => 1024,
'm', 'mb' => (1024 ** 2),
'g', 'gb' => (1024 ** 3),
't', 'tb' => (1024 ** 4),
default => 0
};
return (int)$value;
}
echo getBytes('2GB') . "</br>";
echo getBytes('4tb') . "</br>";
echo getBytes('5345etrgrfd') . "</br>";
echo getBytes('357568336586') . "</br>";
?>