Php Help
antifreeze
The guy with the goods! Join Date: 2003-05-12 Member: 16232Members, Constellation
in Off-Topic
<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?
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
[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].
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.
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-->.
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!!
<!--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.
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-->
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-->
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-->
And I say almost only to cover the few cases where this assumption would be wrong. :P