Forum Moderators: coopster
Question: how do I read my include files and include them in the echoed output?
This all starts from a link in test2.shtml:
<P CLASS="article"><A HREF="#" onClick="openprintwindow()">Print This Page</A></P>
This uses the following JavaScript function to pop open a window and invoke the PHP:
function openprintwindow()
{
/* Retrieve URL of current page, parse out file name */
var URL = unescape(location.href) // get current URL in plain ASCII
var URLstart = URL.lastIndexOf("/") + 1
var URLend = URL.length
var pageName = URL.substring(URLstart,URLend)
pageName = "print.php?PG=" + pageName
/* Open print window and pass in current file name as argument */
OpenWindow=window.open(pageName, "_blank", "height=600,width=600,toolbar=no,scrollbars=yes,menubar=no,location=no,resizable=yes");
}
All of this song-and-dance with passing the filename from the JavaScript was required because when I tested my PHP script in MSIE, $HTTP-REFERRER returned nothing when you call it from an onclick event.
OK, Now the PHP part starts.
print.php contains:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<LINK REL="stylesheet" HREF="styles/print.css" TYPE="text/css">
</HEAD>
<BODY>
<DIV ALIGN="RIGHT" STYLE="text-align:right; padding: 10px;">
<FORM>
<INPUT TYPE="IMAGE" NAME="Print" VALUE="Print"
onClick="javascript:window.print();"
SRC="graphics/button-print.gif"
WIDTH="75" HEIGHT="23" BORDER="0" ALT="Print">
<INPUT TYPE="IMAGE" VALUE="Close"
onClick="javascript:window.close();"
SRC="graphics/button-close.gif"
WIDTH="75" HEIGHT="23" BORDER="0" ALT="Close">
</FORM>
</DIV>
<?php
$startprint = "<!-- startprint -->";
$stopprint = "<!-- stopprint -->";
$read = fopen($PG, "rb");
$rawtext = "";
while(!feof($read)){
$rawtext .= fread($read, filesize($PG));
}
fclose($read);
$begin= strpos($rawtext, "$startprint");
$end= strpos($rawtext, "$stopprint");
$length= $end-$begin;
$rawtext=substr($rawtext, $begin, $length);
function stripNOPRINT($variable)
{
return(preg_replace("%<!-- noprint.*?noprint end -->%si", "", $variable));
}
function replaceINCLUDES($variable)
{
$temptxt = preg_replace('%<!--#include file="%i', '<?php include("/', $variable);
return(preg_replace('%.ssi" -->%i', '.ssi");?>', $temptxt));
}
$PrintText = stripNOPRINT("$rawtext");
$PrintText = replaceINCLUDES("$PrintText");
echo $PrintText;
flush (); //force
?>
</BODY>
</HTML>
---------------
OK... whether I process the includes as shown above or not, the include file text is not actually output into the print popup window.
What am I doing wrong? How would I go about getting my includes, well, included?
[edited by: jatar_k at 12:13 am (utc) on Nov. 9, 2005]
[edit reason] no urls thanks [/edit]
The script also strips out anything that occurs between the comments <!-- noprint start --> and <!-- noprint end -->, which allows me to flag specific items not to be placed into the print-friendly window output.
hmmm, why not call the page you want to parse with a socket or curl then the includes will get processed and you can just work with the fully processed output.
another thought
what I often do is split content out for pages like this so I can include the content in the normal template or I can include in a stripped down print page.
I Googled and have done a little reading about sockets, and I think that's ultimately what I want to do with this script.
In the meantime, here is the workaround that I got working this afternoon:
<?php
//initialize some variables
$rawtext = "";
$headtext = "";
$foottext = "";
function readINPUTFILE($filetoget) //open file and return file contents
{
$retrievedtext = "";
$read = fopen("$filetoget", "rb");
while(!feof($read)){
$retrievedtext .= fread($read, filesize($filetoget));
}
fclose($read);
return($retrievedtext);
}
//read in contents of page to be printed and concatenate to end of $rawtext
$rawtext .= readINPUTFILE("$PG");
//define the start and stop points -- we'll print what's between these tags
$startprint = "<!-- startprint -->";
$stopprint = "<!-- stopprint -->";
//strip out stuff before and after the startprint and stopprint markers
$begin= strpos($rawtext, "$startprint");
$end= strpos($rawtext, "$stopprint");
$length= $end-$begin;
$rawtext=substr($rawtext, $begin, $length);
//open text file with HTML header stuff and store contents in $headtext
$headtext .= readINPUTFILE("printfilehead.ssi");
//open text file with HTML footer stuff and store contents in $foottext
//
$foottext .= readINPUTFILE("printfilefoot.ssi");
//concatenate $headtext, $rawtext, and $foottext together
$rawtext = $headtext . $rawtext . $foottext;
function stripNOPRINT($variable) //strip out stuff between noprint start and noprint end
{
return(preg_replace("%<!-- noprint.*?noprint end -->%si", "", $variable));
}
//process the $rawtext to get rid of noprint stuff
$PrintText = stripNOPRINT("$rawtext");
//open temp file and shove $PrintText contents into it
$outputfilename = "printtemp.shtml";
$resourcehandle = fopen($outputfilename, 'wb') or die("Could not open " . $outputfilename);
fwrite($resourcehandle, $PrintText) or die("Could not write to " . $outputfilename);
fclose($resourcehandle);
//load the temp file in the print-friendly window
$location = "Location: " . $outputfilename;
header($location);
?>