tbare Posted October 31, 2007 Share Posted October 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/75550-solved-read-print-file-to-x-character/ Share on other sites More sharing options...
pocobueno1388 Posted October 31, 2007 Share Posted October 31, 2007 Use the explode function. Take this for example <?php $str = "125485~dds5f56"; $new_str = explode('~', $str); echo $new_str[0]; //prints "125485" echo $new_str[1]; // prints "dds5f56"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75550-solved-read-print-file-to-x-character/#findComment-382191 Share on other sites More sharing options...
tbare Posted October 31, 2007 Author Share Posted October 31, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/75550-solved-read-print-file-to-x-character/#findComment-382202 Share on other sites More sharing options...
Barand Posted October 31, 2007 Share Posted October 31, 2007 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/75550-solved-read-print-file-to-x-character/#findComment-382210 Share on other sites More sharing options...
tbare Posted October 31, 2007 Author Share Posted October 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/75550-solved-read-print-file-to-x-character/#findComment-382248 Share on other sites More sharing options...
Barand Posted October 31, 2007 Share Posted October 31, 2007 Sorry, that does the whole file. I took your post title literally. To process line at a time <?php $text = file('filename.txt'); foreach ($text as $line) { list ($date, $content) = explode ('~', $line); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75550-solved-read-print-file-to-x-character/#findComment-382254 Share on other sites More sharing options...
tbare Posted October 31, 2007 Author Share Posted October 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/75550-solved-read-print-file-to-x-character/#findComment-382256 Share on other sites More sharing options...
Barand Posted October 31, 2007 Share Posted October 31, 2007 It saves having to fopen() and fclose() Quote Link to comment https://forums.phpfreaks.com/topic/75550-solved-read-print-file-to-x-character/#findComment-382268 Share on other sites More sharing options...
tbare Posted October 31, 2007 Author Share Posted October 31, 2007 makes sense... just saved 5 lines of code. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/75550-solved-read-print-file-to-x-character/#findComment-382289 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.