English 中文(简体)
Regex对1024KB改为1MB?
原标题:Regex match 1024KB convert to 1MB?
  • 时间:2010-08-05 21:24:38
  •  标签:
  • regex
  • match

我从该页的索引中拿到档案尺寸,该指数为1024KB,我希望它印刷1MB,而不是1024KB。 (此处完全是注解)

我这样做:

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
";
    logstr("log-c.txt","bad link
");
   }
   else
   {

    preg_match("**/([^/|#<(>;s][0-9]*[s][KB]{2})/**",$index,$match);

     $fsize=$match[1];



  print $fsize."
";
  logstr("log-c.txt","bad link
");
  //logstr("log-c.txt","$caption | $fsize
");
  mysql_query("UPDATE `v2links` SET `checked`= 1 ,`fsize`= $fsize ,`lastcheck`=NOW() WHERE `id`=".$row[0]);
  unset($match);
  }
 }

增 编

最佳回答

你们想要转换是多么准确?

s/0*([0-9]+)[0-9]{3}K/1M/g

超过四位或四位以上数字的最近三位数......但该数字为缩略语,甚至t consider math。 (例如,1999K-> 1M)

一种可能更好的方法是:s/K/000/gs/M/000000/g,将其变为(精减)数量的tes,然后根据你的愿望加以转换。

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.

问题回答

暂无回答




相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签