Jump to content

[SOLVED] read / print file to 'x' character??


tbare

Recommended Posts

Sorry if this has been discussed before, but i can't find an answer anywhere... maybe it can't be done?

 

Here's my issue:

 

Currently, i am using a php script to write information to a text file. Upon submission of the form, the date is printed (form: yyyy.mm.dd), followed by a space, then the information entered in the form.

 

This txt document is then read then printed line by line on a web page.

 

//read data from the news file in /news_input/news.txt and prints it a line at a time

if (!($newsfile = fopen("news_input/news.txt", "r"))) {
die('Cannot open file');
}

while ($line = fgets($newsfile,4096)) {

//read first 10 characters of line an label as date
$date = preg_replace('/(..........).*()/','$1$2',$line);

//read all characters after date and label as content
$content = substr($line, 10);
$news = stripslashes($content);

print "<tr><td valign='top'>";
print "<font color=#000000>$date</font>";
print "<td align='left'> $news";
print "</td>";
print "</tr>";
}
fclose($newsfile);
?>

Note that the date and the info are printed into separate cells of a table.

 

my question: is there a way to do something similar, only reading up to a specified character (say '~' for example), and everything before said character be labeled $date, and everything after said character to be labeled $content?

 

Let me know if you need more informatioin. i do not have access to a sql db, so that's out of the question (free web hosting, can't be picky...) (btw, the website this is on is http://www.wannafork.com).

 

If you want the source code of how i wrote the information to the file, i'll post it, too

Link to comment
Share on other sites

you are a god among men. Thanks!! i was beating my head over this one... my friend will thank you, too... (it took less time to register on this forum, post the question AND get the reply than i spent looking for this answer all day!!)

 

thanks again!

Link to comment
Share on other sites

 

Get the contents of the file first with this, (instead of fgets)

 

$text = file_get_contents ('filename.txt');

 

then explode as pocobueno suggested

 

<?php
$text = 'Twas brillig~and the slthy toves did gyre and gimble in the wabe';    // assume this was in the file

list ($date, $content) = explode ('~', $text);

echo $date;        // -> Twas brillig
echo '<br/>';
echo $content;     // -> and the slthy toves did gyre and gimble in the wabe
?>

Link to comment
Share on other sites

 

Get the contents of the file first with this, (instead of fgets)

 

$text = file_get_contents ('filename.txt');

 

will this return it a line at a time?

 

basically, when info is entered into the txt file, it's in a specified order (date, separator, content, line break). i need to read the info a line at a time until eof.

Link to comment
Share on other sites

what are the benefits of your way over this:

 

<?php

//read data from the news file in /news_input/news.txt and prints it a line at a time

if (!($newsfile = fopen("news_input/news.txt", "r"))) {
die('Cannot open file');
}

while ($line = fgets($newsfile,4096)) {
list ($date, $content) = explode('~', $line);

print "<tr><td valign='top'>";
print "<font color=#000000>$date</font>"; //print date in left cell
print "<td align='left'> $content";               //print content in right cell
print "</td>";
print "</tr>";
}
fclose($newsfile);
?>

 

sorry for the n00bish question, but i'm still somewhat new to php scripting and want to understand what works best aside from taking someone's word for it... (that's how i learn best).

 

Thanks again

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.