Bricktop Posted July 21, 2009 Share Posted July 21, 2009 Hi all, I have a text file which contains testimonials. If you open it up it looks like: You are so great || SomeCompany.com Wow, thanks for the help || Some Body Thanks for that thing you did || SomeOtherCompany.com I'll definitely use you again || Some Person What I'm trying to do is display the text line by line, but also separate the two parts before and after the ||. This is the code I have and it's just not working. $quotesfile = 'file.txt'; $quotescontent = file_get_contents($quotesfile); $lines = explode("\n",$quotescontent); foreach($lines as $line) { explode("||", $line); //Display the quote and the author $content .= '<blockquote class="quotetext">'.$line[0].'</blockquote><br /><br />'; $content .= '<cite class="authortext">'.$line[1].'</cite>'; echo $content; } Where have I gone wrong please? Thanks Link to comment https://forums.phpfreaks.com/topic/166799-solved-reading-text-file-line-by-line/ Share on other sites More sharing options...
rhodesa Posted July 21, 2009 Share Posted July 21, 2009 you aren't using explode() properly...it returns the array. i also shortened up the beginning part: <?php $quotesfile = 'file.txt'; $content = ""; foreach(file($quotesfile) as $line) { list($quote,$author) = explode("||", $line, 2); //Display the quote and the author $content .= '<blockquote class="quotetext">'.$quote.'</blockquote><br /><br />'; $content .= '<cite class="authortext">'.$author.'</cite>'; } echo $content; ?> Link to comment https://forums.phpfreaks.com/topic/166799-solved-reading-text-file-line-by-line/#findComment-879540 Share on other sites More sharing options...
Bricktop Posted July 21, 2009 Author Share Posted July 21, 2009 Thanks rhodesa, it's working perfectly now. Link to comment https://forums.phpfreaks.com/topic/166799-solved-reading-text-file-line-by-line/#findComment-879551 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.