Alternative to fgets (Parsing text file)

Discussion in 'PHP' started by stabbie, Oct 1, 2007.

  1. #1
    Hi there,

    Does anyone know how I can go about parsing a text file (with html tags) so that when I print() the contents, the output will include the html tags.

    For example:

    I have
    
    <?php 
    $filename = "test.txt";
    $fp = fopen($filename, "r") or die("Couldn't open $filename");
    
    while (!feof($fp)){
    $line = fgets($fp);
    echo $line; } 
    ?>
    
    Code (markup):
    which prints out:
    but the contents of the "test.txt" is:
    Thanks heaps in advance.
    James
     
    stabbie, Oct 1, 2007 IP
  2. PoemofQuotes

    PoemofQuotes Peon

    Messages:
    637
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It's echoing everything, so within the page it is probably still spitting out the div tags. What you'll have to do is change the div tags to:
    
    &lt;div&gt;
    
    Code (markup):
    instead. That way it won't make it actual html, but text.

    You could do this with a string replace htmlentities like:
    
    <?php 
    $filename = "test.txt";
    $fp = fopen($filename, "r") or die("Couldn't open $filename");
    
    while (!feof($fp)){
    $line = fgets($fp);
    echo htmlentities($line); } 
    ?>
    
    Code (markup):
     
    PoemofQuotes, Oct 1, 2007 IP
  3. sdemidko

    sdemidko Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #3
    may be you can use the second parameter of fgets. I used to fgets($f, 32000). And to read a file in cycle
     
    sdemidko, Oct 2, 2007 IP
  4. kendo1979

    kendo1979 Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    @sdemidko

    the second parameter is used to tell fgets on how many bytes of data should it get per cycle, which won't solve the problem.
     
    kendo1979, Oct 2, 2007 IP