Php Help

antifreezeantifreeze The guy with the goods! Join Date: 2003-05-12 Member: 16232Members, Constellation
<div class="IPBDescription">Does anyone know?</div> Does anyone know if there is a way in PHP to do the same as what is done <a href='http://www.asp101.com/samples/source.asp' target='_blank'>here</a> in asp?

I need to find some php which will allow me include the php source of a document within anohter document. But i only want to include a certain section of the document which i can specify.

Can anyone help?

Comments

  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    edited November 2004
    <a href='http://au.php.net/manual/en/function.fopen.php' target='_blank'>Yup</a>. Pretty much the same as C. <a href='http://au.php.net/manual/en/ref.pcre.php' target='_blank'>These</a> might help, too.

    [edit]Hmm... if the files aren't too big, you might even try <a href='http://au2.php.net/manual/en/function.readfile.php' target='_blank'>this</a>[/edit].
  • ZeroByteZeroByte Join Date: 2002-11-01 Member: 3057Members
    edited November 2004
    I can't think of an inbuilt function that specifically does what you say, but I'm thinking a combination of <a href='http://www.php.net/manual/en/function.file.php' target='_blank'>file()</a> and <a href='http://ph.php.net/manual/en/function.eval.php' target='_blank'>eval()</a> would do it. Read the file with the source code you want using file() and then have a function string together the lines you want into a single variable. After which, you eval() that variable.

    However, it sounds like it would just be easier if you moved the code you want in the other file to a separate file and then just <a href='http://ph.php.net/manual/en/function.include.php' target='_blank'>included()</a> it into both files. You've given me an idea for a function though, so I might actually go code this.
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    edited November 2004
    As far as I can tell that asp code just outputs the target file rather than execute it. I could be wrong, of course; I've never done any work in asp.

    If you <i>do</i> want to execute the target file: use include(). If you only want to execute part of the file (and you really <i>really</i> want to write evil and foul code rather than use functions) you could copy the chunk you want into a temporary file, then include() the temp file <!--emo&;)--><img src='http://www.unknownworlds.com/forums/html//emoticons/wink-fix.gif' border='0' style='vertical-align:middle' alt='wink-fix.gif' /><!--endemo-->.
  • antifreezeantifreeze The guy with the goods&#33; Join Date: 2003-05-12 Member: 16232Members, Constellation
    edited November 2004
    <!--QuoteBegin-SoulSkorpion+Nov 17 2004, 02:12 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Nov 17 2004, 02:12 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> As far as I can tell that asp code just outputs the target file rather than execute it. I could be wrong, of course; I've never done any work in asp. <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    Correct. It convert's the code to HTML, which i am wanting to do so i can display some source code.

    The way the ASP one worked is it looked for the tags
    <!-- BEGIN SCRIPT --> ..... <!-- END SCRIPT -->
    then displayed anything that is within the 2 tags, which is exactly what i am wanting it to do, but in PHP.

    ZeroByte if you could code something like that it would be Awesome!!
  • ZeroByteZeroByte Join Date: 2002-11-01 Member: 3057Members
    Oh.. I thought you wanted to take part of the source code and actually execute it, which is what I coded up. But in any case, the code will only require a little bit of change.

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    function include_lines($filename, $line_start, $line_end)
    {
       if ($includedFile = @file($filename))
       {
           // Offset line start to correspond with array line count
           $line_start--;
           $eval_this;
           for ($i=$line_start; $i<$line_end; $i++)
           {
               $eval_this .= $includedFile[$i];
           }
           return $eval_this;
       }
       else
           return FALSE;
    }
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    I'm not quite sure if the line breaks will be included by my code, and I never really tested it so take that with a grain of salt, although the theory seems sound to me.
  • antifreezeantifreeze The guy with the goods&#33; Join Date: 2003-05-12 Member: 16232Members, Constellation
    Ok after some arsing about i managed it. I checked the forums before you posted ZeroByte then after i managed it i checked again and the solution was there beating me in the face.

    I don't know if the way i decided to do it is worse in any way, you can tell me that.

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->function includePage($file) {
     $StartPos = strpos(file_get_contents($file),"<!--PHPread-->");
     $EndPos = strpos(file_get_contents($file),"<!--PHPstop-->");

     echo substr(file_get_contents($file),$StartPos,($EndPos - $StartPos));
    }<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
  • ZeroByteZeroByte Join Date: 2002-11-01 Member: 3057Members
    Yours is a much more elegant solution, and it's especially good if you're going to be changing the source code of the file you're reading from. Then again I really didn't look through that ASP code so I was only hazarding a guess at what you wanted.

    However, you can improve your code performance wise by only reading the file once and dumping it to a variable instead of reading it every single time.

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    function includePage($file) {
    $fileContents = file_get_contents($file);
    $StartPos = strpos($fileContents,"<!--PHPread-->");
    $EndPos = strpos(f$fileContents,"<!--PHPstop-->");

    echo substr($fileContents,$StartPos,($EndPos - $StartPos));
    }
    <!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
  • antifreezeantifreeze The guy with the goods&#33; Join Date: 2003-05-12 Member: 16232Members, Constellation
    Cheers for your help. I have updated my code so it only reads the file once. I had to change the variable "$fileContents" though because it didn't seem to like it for whatever reason.

    Works now though <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html//emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo-->
  • DOOManiacDOOManiac Worst. Critic. Ever. Join Date: 2002-04-17 Member: 462Members, NS1 Playtester
    As a general rule of thumb, ANYTHING that can be done w/ a web server language (be it PHP, Perl, JSP, or ASP) can be done w/ any other web server language. Certain things may be much easier in one vs the other, but its (almost) always possible.

    And I say almost only to cover the few cases where this assumption would be wrong. :P
Sign In or Register to comment.