[SOLVED] reading from a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

[SOLVED] reading from a file

Post by hward »

Hi I can read my file ok with this code

Code: Select all

<?php
    if (!($fp = fopen("/proc/mdstat", "r")))  
        exit("Unable to open the input file.");
    while (!feof($fp))  
    &#123;  
        $c = fgetc($fp);  
        print $c;  
    &#125;
    fclose($fp);
?>
Personalities : [raid1] read_ahead 1024 sectors md0 : active raid1 hde1[1] hdg1[0] 39078016 blocks [2/2] [UU] [======>..............] resync = 34.9% (13670876/39078016) finish=41.0min speed=10324K/sec unused devices:

All I want out of it is
resync = 34.9%
finish=41.0min

Can I pull out just that part of the file?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$str = file_get_contents("/proc/mdstat");
preg_match("/.*resync = ([0-9.]*)%.*finish=([0-9.]*)min/m", $str, $matches);
echo "resync = $matches[1]%\nfinish=$matches[2]min\n";
something like that.
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

that works fine but when it is finished it shows

resync=% finish= min

how can you make it show

resync=100% finish=complete

when it is finished and theres nothing to show
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

it means you need to tweak the regexp I gave to you to match your sting.
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

I guess I don't understand what you mean

you mean like if else stuff?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

does it work while the process you monitor still running? something like
resync = 34.9%
finish=41.0min

?
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

I got it working with and if else

thanks
Post Reply