- strstr: documentation ( source)
- round: documentation ( source)
- str_replace: documentation ( source)
<?php
$pc = 0;
$gigs = 150;
$str = '1001M';
if (strstr($str[0], 'G'))
{
$type = "G";
$pc = str_replace('G','',$str[0]) / $gigs * 100;
$pc = round($pc,2);
}
elseif (strstr($str[0], 'M'))
{
$type = "G";
$pc = str_replace('M','',$str[0]) / $gigs;
$pc = round($pc,2);
}
if ($pc >= 100 && $type == "G") { $pc = 100; }
echo $pc;