Current location: Hot Scripts Forums » Programming Languages » PHP » how to separate and read data from text file


how to separate and read data from text file

Reply
  #1 (permalink)  
Old 12-14-04, 12:11 AM
robbydweb robbydweb is offline
Newbie Coder
 
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
how to separate and read data from text file

I just started learning php and need little help.
If i have a text file like this:

name1|location1|message1
name2|location2|message2
ect...

How can I read 1 line at a time, separate each section and display each value ? Then after, goes to next line and repeats until all lines are read.

I know how to read a whole text file, but dont know
how to break it up and display each part separaetly.
any help would be appreciated, thanks
Reply With Quote
  #2 (permalink)  
Old 12-14-04, 12:20 AM
greybird greybird is offline
Newbie Coder
 
Join Date: Dec 2004
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Have answered your question on devforums
Reply With Quote
  #3 (permalink)  
Old 12-14-04, 12:40 AM
irfanbaba's Avatar
irfanbaba irfanbaba is offline
Newbie Coder
 
Join Date: Dec 2004
Location: Karachi, Pakistan
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

PHP Code:

<?php


function get_news($filepath$newsid)
{
$datafile fopen("$filepath/news/$newsid.txt","r");
$data fread($datafile1000000);
list(
$author$email$date$subject$body) = explode("|",$data);
$body stripslashes("$body");
$subject stripslashes("$subject");
echo 
"<a href=\"mailto:$email\">$author</a> -- $date -- $subject<hr>$body<p>";
}
?>
sample file:

AdministratorMax|admin@somesite.com|Tuesday, March 5th @ 5:45 PM EST|Site Going Down Tomarrow|Well, folks, I\'m sorry to say that the site will indeed be down tomarrow for most of the day. Hang in there.


hope this may help you out
__________________
Web Programmer
web: http://www.netxs.com.pk
email: irfanmail@gmail.com
msn : irfanbaba@hotmail.com
Reply With Quote
  #4 (permalink)  
Old 12-17-04, 02:22 AM
mo7kvi4 mo7kvi4 is offline
New Member
 
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by robbydweb
I just started learning php and need little help.
If i have a text file like this:

name1|location1|message1
name2|location2|message2
ect...

How can I read 1 line at a time, separate each section and display each value ? Then after, goes to next line and repeats until all lines are read.

I know how to read a whole text file, but dont know
how to break it up and display each part separaetly.
any help would be appreciated, thanks
Simple way for small text files (about 10Mb):
PHP Code:

<?

$arr
=file("data.txt");

    foreach(
$arr as $str)
    {
    list(
$name,$location,$message)=explode("|",$str);

    echo 
"<li>".$name;
    echo 
"<li>".$location;
    echo 
"<li>".$message;

    echo 
"<hr>";
    }
?>
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 05:19 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.