By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

Reading a text file in PHP

P: 12
Hi,

I'm currently reading a text file via PHP which, in itself, is very easy. However I want to specifically get one word from the text file and assign it as a variable and I'm struggling like mad! The text file is just a ping result, as follows:

Pinging www.l.google.com [64.233.183.147] with 32 bytes of data:

Reply from 64.233.183.147: bytes=32 time=34ms TTL=249
Reply from 64.233.183.147: bytes=32 time=32ms TTL=248
Reply from 64.233.183.147: bytes=32 time=31ms TTL=248
Reply from 64.233.183.147: bytes=32 time=32ms TTL=249

Ping statistics for 64.233.183.147:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 31ms, Maximum = 34ms, Average = 32ms


What I really want to do is get the word reply out of the text file so I can do a simple if statement that says If $variable="Reply" then the ping has been successful otherwise it has failed.

Does this make sense? Can anyone point me in the right direction?
Mar 24 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
Yes, it's really easy, use the functions file and split

so (assuming that the file is called pingReply.txt)

[php]
$file = file( 'pingReply.txt' );
$repliesFound = 0;

foreach( $file as $line )
{
$tokens = split( ' ', $line );

if ( strcasecmp($tokens[0], 'Reply') == 0 )
{
$repliesFound++;
}
}

echo( "Got $repliesFound replies to the ping\n" );
[/php]
Mar 24 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.