Jump to content

Php Reading A Txt File


CanMan2004

Recommended Posts

Hi all

I have a site which creates a log txt file for me, inside it, it stores the name of the person who logged in and there age, for example the txt file might look like

username - Dave Smith
age - 34

How can I get php to open the file and read the username piece of information inside the txt file and ignore the rest? I have read some information on reading files but all my examples seem to fail.

Any help would be great

Thanks in advance

Dave
Link to comment
Share on other sites

Assuming the exact contents of your log.txt file are
[code]username - Dave Smith
age - 34[/code]

Try this:

[code]<?
$file = "log.txt";
$size = filesize($file);
$fh = fopen ($file, 'r');
$data = fread ($fh, $size);
fclose($fh);
$lines = explode("\n",$data);
$pieces = explode(" - ",$lines[0]);
$username = $pieces[1];
echo $username;
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.