I'm having a long text in a string:
$data = "899.3MB 976.4MB 874.7MB 985.6MB 1.2GB 691.3MB 1.4GB 975.4MB 1.0GB 712.8MB 879.8MB 908.6MB 912.1MB 887.5MB 950.4MB 1.4GB 749.9MB 851.2MB 750.8MB 813.7MB 1.2GB 700MB 998.5MB 1010.8MB 989.7MB 1.1GB 797.5MB 699.5MB 683.7MB 1.0GB 958.2MB 1.1GB 1.0GB 1.0GB 1.0GB 1.1GB 1.0GB 1.0GB 846MB 873MB 753MB 1.1GB 970MB 749.9MB 750MB 650MB 845MB 1.1GB 1.3GB 1.2GB 1.1GB 867MB 940MB 946MB 909MB 909MB 913MB 893MB 892MB 889MB 730MB 848MB 855MB 1.15GB 853MB 765MB 1.2GB 863MB 838MB 753MB 960MB 868MB 967MB 1.06GB 751MB 1GB 1.2GB 920MB 1.2GB 897MB 1009.15MB 764MB 694MB 967MB 989.9MB 898MB 938MB 879.76MB 879.76MB 999.78MB 1.3GB 1.1GB 1.09GB"
How can I arrange them in different variables after sorting them. Like $mb will contain only those values which ends with MB and $gb will contain only those values which ends with GB. And also I don't want MB and GB in output I just want the float or int values so that I can Add them.
I'm importing this data from MySql database in which there is a column named file_size
while ($data = mysqli_fetch_array($records))
{
$file_size_ext = str_replace(' ', '',$data['file_size'])." ";
}
mysqli_close($db);
?>
I tried to arrange them separately by echo substr("$file_size_ext",-2) =='GB' but there's nothing like this in PHP 😅.
I just want to separate them into MB and GB so that I can add or get sum of the array "separately".
I should probably modify my database and write all the file sizes in same unit, then it will be much more easier..
You could use something like the following :
$data = "899.3MB 976.4MB 874.7MB 985.6MB 1.2GB 691.3MB 1.4GB 975.4MB 1.0GB 712.8MB 879.8MB 908.6MB 912.1MB 887.5MB 950.4MB 1.4GB 749.9MB 851.2MB 750.8MB 813.7MB 1.2GB 700MB 998.5MB 1010.8MB 989.7MB 1.1GB 797.5MB 699.5MB 683.7MB 1.0GB 958.2MB 1.1GB 1.0GB 1.0GB 1.0GB 1.1GB 1.0GB 1.0GB 846MB 873MB 753MB 1.1GB 970MB 749.9MB 750MB 650MB 845MB 1.1GB 1.3GB 1.2GB 1.1GB 867MB 940MB 946MB 909MB 909MB 913MB 893MB 892MB 889MB 730MB 848MB 855MB 1.15GB 853MB 765MB 1.2GB 863MB 838MB 753MB 960MB 868MB 967MB 1.06GB 751MB 1GB 1.2GB 920MB 1.2GB 897MB 1009.15MB 764MB 694MB 967MB 989.9MB 898MB 938MB 879.76MB 879.76MB 999.78MB 1.3GB 1.1GB 1.09GB";
$res = preg_split('/\s+/', $data);
$gb = [];
$mb = [];
foreach($res as $r) {
if(strpos($r, 'MB') !== false) {
$to_add = str_replace('MB', '', $r);
$mb[] = (float) $to_add;
} else if(strpos($r, 'GB') !== false) {
$to_add = str_replace('GB', '', $r);
$gb[] = (float) $to_add;
}
}
var_dump(array_sum($gb));
var_dump(array_sum($mb));
regex is a short solution
\d+ -> matches digits
. -> matches dot
(.\d+)? -> it say pattern could has .1 .2 or not
<?php
$data = "899.3MB 976.4MB 874.7MB 985.6MB 1.2GB 691.3MB 1.4GB 975.4MB 1.0GB 712.8MB 879.8MB 908.6MB 912.1MB 887.5MB 950.4MB 1.4GB 749.9MB 851.2MB 750.8MB 813.7MB 1.2GB 700MB 998.5MB 1010.8MB 989.7MB 1.1GB 797.5MB 699.5MB 683.7MB 1.0GB 958.2MB 1.1GB 1.0GB 1.0GB 1.0GB 1.1GB 1.0GB 1.0GB 846MB 873MB 753MB 1.1GB 970MB 749.9MB 750MB 650MB 845MB 1.1GB 1.3GB 1.2GB 1.1GB 867MB 940MB 946MB 909MB 909MB 913MB 893MB 892MB 889MB 730MB 848MB 855MB 1.15GB 853MB 765MB 1.2GB 863MB 838MB 753MB 960MB 868MB 967MB 1.06GB 751MB 1GB 1.2GB 920MB 1.2GB 897MB 1009.15MB 764MB 694MB 967MB 989.9MB 898MB 938MB 879.76MB 879.76MB 999.78MB 1.3GB 1.1GB 1.09GB";
preg_match_all('~\d+(\.\d+)?(?=MB)~', $data, $mb);
preg_match_all('~\d+(\.\d+)?(?=GB)~', $data, $gb);
$mb = $mb[0] ?? [];
$gb = $gb[0] ?? [];
First you'll want to split that string into the individual entries. You do that with explode().
$entries = explode(" ", $data);
Then you'll want to create your arrays for your different storage units and iterate through the $entries array:
$mb = [];
$gb = [];
foreach($entries as $val)
{ }
Then in the for loop, you can just search for the units with stripos(). You can also strip out the unit here with str_ireplace().
if(stripos($val, "mb") !== false)
$mb[] = str_ireplace("mb", "", $val);
else if(stripos($val, "gb" !== false)
$gb[] = str_ireplace("gb", "", $val);
And put it all together:
$entries = explode(" ", $data);
$mb = [];
$gb = [];
foreach($entries as $val)
{
if(stripos($val, "mb") !== false)
$mb[] = str_ireplace("mb", "", $val);
else if(stripos($val, "gb" !== false)
$gb[] = str_ireplace("gb", "", $val);
}