Jump to content

[SOLVED] Reading text file line by line


Bricktop

Recommended Posts

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

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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.