开发者

Am I using preg_replace correctly (PHP)?

I think I have this right but I would like someone to verify that.

function storageAmmount()
{开发者_JS百科
    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?


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.

function storageAmmount(){    
    $storage = filesize('/web/'.$user);
}


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 the preg_replace together would be better achieved using a single preg_match.

Something like this would do the trick:

$matches = preg_match('/Size: (\d+)/',$storage);
$storage = $matches[1];

The (\d+) picks up any number of digits and puts them into an element in the $matches array. Putting Size: 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; just explode() on a space character and pick up the second element. No regex required.


The best usage of regex is

// preg_match solution    
$storage = 'Size: 4096 Blocks: 8 IO Block: 4096 directory';
if (preg_match('/Size: (?P<size>\d+)/', $storage, $matches)) {
    return matches['size'];
}

But if you are doing it localy, you can use th php function stat

// php stat solution
$f = escapeshellcmd($user);
if ($stat = @stat('/web/'.$f.'/log/storage')) {
    return $stat['size'];
}


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:

function storageAmount($user) {
    system(escapeshellcmd('stat /web/'.$user.'/ | grep Size: > /web/'.$user.'/log/storage'));
    $storageChunks = explode(' ', file_get_contents('/web/'.$user.'/log/storage'));
    return $storageChunks[1];
}

Incidentally:

  1. 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.

  2. You should really use escapeshellcmd on all commands passed to system/exec, etc.

  3. 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.

  4. 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.)

  5. 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

$storage = preg_replace('Block:.*','',$storage)

?

Even better would be to use

 function storageAmmount()
 {
   exec('stat /web/'.$user.'/ | grep Size:',$output);
   preg_match("/Size: ([0-9]*).*/",$output[0],$matches);
   return $matches[1];
 }

(tested on my machine)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜