Code needed to make PHP read from text file Properly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kvazar
    New Member
    • Jul 2007
    • 6

    Code needed to make PHP read from text file Properly

    Hello,
    I am new to php, and I need some help.
    I am trying to write a script for a web site.
    Text I need to display is in a .txt file, and I can't add breaks to it.
    I wrote a function that will open my files. When text is displaid in html, it is all in one line. I can not find code to add my function to take care of the problem.
    can sombody please help?
    Thank you
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    • How is your text file contents apperaing?
    • Where is the code that you made so far?
    Please post them here!

    Comment

    • mwasif
      Recognized Expert Contributor
      • Jul 2006
      • 802

      #3
      Text I need to display is in a .txt file, and I can't add breaks to it.
      Please explain.

      Use nl2br() for line breaks.

      Comment

      • kvazar
        New Member
        • Jul 2007
        • 6

        #4
        Here is function code I wrote

        <?php
        function open_question($ category) {

        $file = fopen($category , 'r') or die("Can't open file");
        $theData = fread($file, 6800);
        fclose($file);
        echo $theData;
        }
        ?>

        <html>
        <body>
        <?php
        open_question(" ./text.txt");{
        ?>
        </body>
        </html>


        text.txt just has a bunch of text in it, nothing significant.
        for example
        text.txt hassuch text in it:


        I
        need
        help

        And this is exsactly how I want it to show on my site.
        But instead it will all be in one line, like "I need help", and I don't what that!

        Hope this explanation helps

        Comment

        • mwasif
          Recognized Expert Contributor
          • Jul 2006
          • 802

          #5
          Use nl2br().
          [PHP]<?php
          nl2br(open_ques tion("./text.txt"));
          ?>[/PHP]

          Where you want to print the braces?

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            Still your Question is not clear to me.anyway is this the thing you need.?If not please post back.

            php script
            [code=php]
            <?php
            function open_question($ category) {
            $file = fopen($category , 'r') or die("Can't open file");
            $theData = fread($file, 6800);
            fclose($file);
            $delimiter = "\n";
            $splitcontents = explode($delimi ter, $theData);
            foreach( $splitcontents as $record )
            {
            echo $record.'<br>';
            }
            }
            open_question(" ./text.txt");
            ?>
            [/code]

            text.txt
            Code:
            I
            need sdsds oklsdkdkskfldsf
            help
            ldjkfgdlkglkdflgl

            Comment

            • kvazar
              New Member
              • Jul 2007
              • 6

              #7
              Yes!!! That has worked perfectly.
              I am sorry for being a bit unclear with my questions. But yes! That is exsactly what I needed.

              Now, is there also a way for me to add code that will for esample omit line 3.help?

              thanks

              Comment

              • ak1dnar
                Recognized Expert Top Contributor
                • Jan 2007
                • 1584

                #8
                Originally posted by kvazar
                Yes!!! That has worked perfectly.
                I am sorry for being a bit unclear with my questions. But yes! That is exsactly what I needed.

                Now, is there also a way for me to add code that will for esample omit line 3.help?

                thanks
                Do you want to omit just only line number 3 ?
                Is there any other text input after 3rd line in your original text file.

                Comment

                • kvazar
                  New Member
                  • Jul 2007
                  • 6

                  #9
                  Ok, here is an exsample of something like I would use for my text file.

                  QUESTION
                  BLAH BLAH BLAH BLAH BLAH BLAH BLAH
                  FIGURE
                  None
                  OPTIONS
                  1
                  10
                  100
                  30
                  40
                  ANSWER
                  D


                  All of this information that you see has, and will be in the text file. Though I do not need to display all of it.
                  I would need to get rid of lines QUESTION, FIGURE, None, and of course i do not want my ANSWER line with an answer to show.
                  How do I get rid of that.
                  As I have mentioned, all this has to be in my text file.
                  Thanks

                  Comment

                  • kvazar
                    New Member
                    • Jul 2007
                    • 6

                    #10
                    Would anyone have any idea on how I could posibly do this, or if it is possible at all.
                    Thanks

                    Comment

                    • tscott
                      New Member
                      • Jul 2007
                      • 22

                      #11
                      Try using Explode to break it up into an array via line breaks.Then you can only echo the lines you want

                      example (using spaces)
                      [php]
                      <?php
                      $getfile = file_get_conten ts('textfile.tx t');
                      $filearray = explode(' ' , $getfile);
                      echo $filearray[0] . ' ' . $filearray[4] , ' ' . $filearray[2];
                      // Say the contents is:I random pie dog like//
                      //This would say: I like pie//
                      ?>
                      [/php]

                      Comment

                      Working...