PHP question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Donners
    Confirmed User
    • Jun 2004
    • 689

    #1

    PHP question

    Is there a way to use php include command to include only a part of a file?

    For exampel if a html file has two lines in it

    LINE ONE
    LINE TWO

    I want to use php include to only include LINE TWO in my website.
  • Calvinguy
    Confirmed User
    • Oct 2002
    • 1752

    #2
    There are several options. The 'file' function might be a good option for you.

    Comment

    • schneemann
      Confirmed User
      • Oct 2006
      • 749

      #3
      It depends on whether you it will only ever have two lines in it, or if you know exactly what that line contains. In any case, it sounds like a bit inefficient to be doing anything I can think of just to include one line of text from a two line file.
      Deranged World

      Comment

      • Donners
        Confirmed User
        • Jun 2004
        • 689

        #4
        Originally posted by Calvinguy
        There are several options. The 'file' function might be a good option for you.
        I'm not good with php at all, could you explain a little more?

        Comment

        • Donners
          Confirmed User
          • Jun 2004
          • 689

          #5
          Originally posted by schneemann
          It depends on whether you it will only ever have two lines in it, or if you know exactly what that line contains. In any case, it sounds like a bit inefficient to be doing anything I can think of just to include one line of text from a two line file.
          I have a html file with 74 sections in it and want to include each section in its own page.

          So 74 pages will take information from a single html page instead of taking it from 74 other pages.

          Comment

          • Calvinguy
            Confirmed User
            • Oct 2002
            • 1752

            #6
            Originally posted by Donners
            I'm not good with php at all, could you explain a little more?

            $lines = file('yourfile.html');
            echo $lines[1]; // will print line two from your included file

            Comment

            • schneemann
              Confirmed User
              • Oct 2006
              • 749

              #7
              Originally posted by Donners
              I have a html file with 74 sections in it and want to include each section in its own page.

              So 74 pages will take information from a single html page instead of taking it from 74 other pages.
              Maybe I'm having a hard time visualizing what you're after.
              Here's what I think: Put it in a database. A database will be FAR faster.
              file() will read all 74 sections into memory at once whereas with a database you can grab the specific one you want.
              Deranged World

              Comment

              • Donners
                Confirmed User
                • Jun 2004
                • 689

                #8
                Originally posted by Calvinguy
                $lines = file('yourfile.html');
                echo $lines[1]; // will print line two from your included file
                Almost what Im looking for, but instead of choosing the lines I would like to choose a section (with multiple lines) from the included file.

                Like a link anchor that defines a section of a page. And I want to include the anchor section.

                Comment

                • joehoya
                  Registered User
                  • Feb 2006
                  • 82

                  #9
                  Quick answer is to modify your big html page so that each of the 74 sections is encased between a <div> tag. each <div> tag will include an id attribute (e.g. id="73") which will identify the section.

                  From here you could use the xml parser in php5 to build a solution that might last.

                  If you just want something quick and dirty, assuming you aren't using <div> tags anywhere else in your big html file. you could write a regular expression that pulls all data beginning with <div id="73"> and ending with the first instance of </div>.

                  Comment

                  • Donners
                    Confirmed User
                    • Jun 2004
                    • 689

                    #10
                    Originally posted by joehoya
                    Quick answer is to modify your big html page so that each of the 74 sections is encased between a <div> tag. each <div> tag will include an id attribute (e.g. id="73") which will identify the section.

                    From here you could use the xml parser in php5 to build a solution that might last.

                    If you just want something quick and dirty, assuming you aren't using <div> tags anywhere else in your big html file. you could write a regular expression that pulls all data beginning with <div id="73"> and ending with the first instance of </div>.

                    Thanks for the answer.

                    I manage to pull this code together:

                    Code:
                    <?php
                    $start='<div id="73">';
                    
                    $end='</div>';
                    
                    $page='test2.html';
                    
                    $fp=fopen($page,'r');
                    
                    $cont=fread($fp,filesize($page));
                    
                    preg_match("/$start(.*)$end/s",$cont,$match);
                    
                    $info=$match[0];
                    
                    print($cont[1]);
                    ?>
                    But it doesnt work, any ideas?

                    Comment

                    • Donners
                      Confirmed User
                      • Jun 2004
                      • 689

                      #11
                      Sorted it out, this code worked for me.

                      Code:
                      <?php
                      $start='<!-- START HTML DATA -->';
                      
                      $end='<!-- END HTML DATA -->';
                      
                      $page='yay.html';
                      
                      $fp=fopen($page,'r');
                      
                      $cont=fread($fp,filesize($page));
                      
                      preg_match("/$start(.*)$end/s",$cont,$match);
                      
                      $info=$match[0];
                      
                      fclose($cont);
                      print $info;
                      
                      ?>
                      Thanks for the help.

                      Comment

                      • joehoya
                        Registered User
                        • Feb 2006
                        • 82

                        #12
                        Any Time!

                        Comment

                        Working...