Regex match 1024KB convert to 1MB?
I get the file size from the index of the page, it's 1024KB and I want it to print 1MB stead of 1024KB, what should I do? (completely noob here)
I got this:
if($row[2]==1) // Rapidshare Check
{
$index=getpage($row[1]);
if(strpos($index,"FILE DOWNLOAD")===false) //check if page contains the word file download if not = bad link
{
mysql_query("UPDATE `v2links` SET `checked`='-1',`lastcheck`=NOW() WHERE `id`=".$row[0]);
print "bad link\n";
logstr("log-c.txt","bad link\n");
}
else
{
preg_match("**/([^\/\|\#\<\(\>\;\s][0-9]*[\s][KB]{2})/**",$index,$match);
$fsize=$match[1];
print $fsize."\n";
logstr("log-c.txt","bad link\n");
//logstr("log-c.txt","$caption | $fsize\n");
mysql_query("UPDATE `v2links` SET `checked`='1',`fsize`='$fsiz开发者_C百科e',`lastcheck`=NOW() WHERE `id`=".$row[0]);
unset($match);
}
}
Thanks
How accurate do you want the conversion to be?
s/0*([0-9]+)[0-9]{3}K/\1M/g
will lop off the last three numbers of a four or greater digit size... but it's a truncation, and doesn't even consider math. (For example, 1999K -> 1M)
A possibly better method would be to s/K/000/g
and s/M/000000/g
to turn it into a (crude) number of bytes, and then convert as you want back down.
The best possible method would be to process it, if it has a M multiply by 1024*1024; if it has K multiply by 1024. (if it has G, multiply by 1024*1024*1024). Then, process the resulting size however you're trying to. --Note that it'd be a good plan to store file size as an integer, rather than a string.
For processing and output, a series of if's are probably good enough, and you can set precise tollerances for how big something must be to be displayed as M instead of K. --Or if you just want M, there's no if, and you just divide by 1024*1024.
精彩评论