我是否正确使用了 preg_replace (PHP)?
我认为我有这个权利,但我希望有人验证这一点。
function storageAmmount()
{
system('stat /web/'.$user."/ | grep Size: > /web/".$user."/log/storage");
$storage = fopen('/web/'.$user.'/log/storage');
$storage = str_replace('Size:', " ", $storage);
$storage = preg_replace('Blocks:(((A-Z), a-z), 1-9)','',$storage);
}
这是文本文件中的行:
Size: 4096 Blocks: 8 IO Block: 4096 directory
我试图获取收益“Size:”的数值,单词 Size:其他一切对我来说都是无用的。
我主要看的是preg_replace
。是我一个人的问题还是正则表达式有点令人困惑?有什么想法。感谢您提前提供的任何帮助。
干杯!,
Phill
好吧,
函数现在是这样的:
function storageAmmount()
{
$storage = filesize('/web/'.$user.'/');
$final = $storage/(1024*1024*1024);
return $final;
}
我应该把 number_format() 放在哪里,我不确定它是否会出现在方程中或返回语句中。我在两者中都尝试过它,它返回的只是“0.00”。
V1。
function storageAmmount()
{
$storage = filesize('/web/'.$user.'/');
$final = number_format($storage/(1024*1024*1024), 2);
return $final;
}
或V2。
function storageAmmount()
{
$storage = filesize('/web/'.$user.'/');
$final = $storage/(1024*1024*1024);
return number_format($final, 2);
}
两者都不起作用,并且都返回“0.00”。有什么想法吗?
I think I have this right but I would like someone to verify that.
function storageAmmount()
{
system('stat /web/'.$user."/ | grep Size: > /web/".$user."/log/storage");
$storage = fopen('/web/'.$user.'/log/storage');
$storage = str_replace('Size:', " ", $storage);
$storage = preg_replace('Blocks:(((A-Z), a-z), 1-9)','',$storage);
}
This is the line in the text file:
Size: 4096 Blocks: 8 IO Block: 4096 directory
I am trying to get just the numeric value the proceeds "Size: ", the word Size: and everything else is usless to me.
I am mainly looking at the preg_replace
. Is it just me or is regex a tad bit confusing? Any thoughts. Thanks for any help in advance.
Cheers!,
Phill
Ok,
Here is what the function looks like now:
function storageAmmount()
{
$storage = filesize('/web/'.$user.'/');
$final = $storage/(1024*1024*1024);
return $final;
}
Where would I put the number_format(), I am not really sure if it would go in the equation or in the return statement. I have tred it in both an all it returns is "0.00".
V1.
function storageAmmount()
{
$storage = filesize('/web/'.$user.'/');
$final = number_format($storage/(1024*1024*1024), 2);
return $final;
}
or V2.
function storageAmmount()
{
$storage = filesize('/web/'.$user.'/');
$final = $storage/(1024*1024*1024);
return number_format($final, 2);
}
neither work and they both return "0.00". Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来您正在尝试获取文件的大小(以字节为单位)。如果是这样,为什么不直接使用 PHP 的 filesize 函数,该函数将文件名作为它的参数并返回文件的大小(以字节为单位)。
Looks like you are trying to get the size of the file in bytes. If so why not just use the filesize function of PHP which takes the file name as its argument and returns the size of the file in bytes.
不,您没有正确使用
preg_replace
。您的代码中有很多误解;要纠正它意味着我必须解释正则表达式如何工作的基础知识。我真的建议您阅读一些有关该主题的入门读物。这里有一个很好的: http://www.regular-expressions.info/
事实上,什么您尝试在此处将
str_replace
和preg_replace
一起使用,使用单个preg_match
可以更好地实现。像这样的事情就可以解决问题:
(\d+)
拾取任意数量的数字并将它们放入 $matches 数组中的元素中。将Size:
放在前面会强制它仅识别输入字符串中紧接在 Size: 之后的数字。如果您的输入字符串的格式与您所描述的方式一致,您也可以根本不使用任何
preg_
函数;只需在空格字符上使用explode()
并选取第二个元素。不需要正则表达式。No, you're not using
preg_replace
properly.There's a lot of misunderstanding in your code; to correct it would mean I'd have to explain the basics of how Regex works. I really recommend reading a few primers on the subject. There's a good one here: http://www.regular-expressions.info/
In fact, what you're trying to do here with the
str_replace
and thepreg_replace
together would be better achieved using a singlepreg_match
.Something like this would do the trick:
The
(\d+)
picks up any number of digits and puts them into an element in the $matches array. PuttingSize:
in front of that forces it to only recognise the digits that are immediately after Size: in your input string.If your input string is consistently formatted in the way you described, you could also do it without using any
preg_
functions at all; justexplode()
on a space character and pick up the second element. No regex required.正则表达式的最佳用法是
但是如果您在本地执行此操作,则可以使用 php 函数
stat
The best usage of regex is
But if you are doing it localy, you can use th php function
stat
记住您已经在使用字符串操作这一事实(不太明白为什么 - 一个正则表达式可以处理这一切),我不知道为什么您不继续沿着这条路走。
例如使用explode:
顺便说一句:
$user 变量不存在于您的函数范围内 - 您要么需要像我一样将其作为参数传递,要么将其设为全局变量。
您确实应该在传递给 system/ 的所有命令上使用 escapeshellcmd exec等。
您错误地使用了 fopen。 fopen 返回一个资源,然后您需要在使用 fclose 之前通过 fread 等读取该资源。也就是说,我已将其替换为 file_get_contents为您执行打开/读取/关闭操作。
我不太确定您正在尝试使用系统命令执行什么操作,但我将其保留为原样。但是,您可以直接将 grep 的结果作为最后一个字符串输出返回,而不必将其输出到文件中。 (这是 system 命令返回的内容。)您还混合了' 和 " 作为字符串分隔符 - 这是行不通的。(只需一致使用一个即可。)
我怀疑您实际上想要“
df --si /web/'.$user.'/'
”命令的最后一行,否则您将总是返回值 4096。Bearing in mind the fact that you're already using string manipulation (don't quite get why - a single regular expression could handle it all), I don't know why you don't proceed down this path.
For example using explode:
Incidentally:
The $user variable doesn't exist within the scope of your function - you either need to pass it in as an argument as I've done, or make it a global.
You should really use escapeshellcmd on all commands passed to system/exec, etc.
You're using fopen incorrectly. fopen returns a resource which you then need to read from via fread, etc. prior to using fclose. That said, I've replaced this with file_get_contents which does the open/read/close for you.
I'm not really sure what you're attempting to do with the system command, but I've left it as-is. However, you could just get the result of the grep back directly as the last string output rather than having to output it to a file. (This is what the system command returns.) You're also mixing ' and " as string delimiters - this won't work. (Just use one consistently.)
I suspect you actually want to the final line of "
df --si /web/'.$user.'/'
" command as otherwise you'll always be returning the value 4096.你尝试过吗
?
更好的是使用
(在我的机器上测试过)
have you tried
?
Even better would be to use
(tested on my machine)