PHP Printer-Friendly Page

Discussion in 'PHP' started by swagner, Oct 14, 2007.

  1. #1
    Hello,

    I am trying to create a page on my website that dynamically extracts all of the contents in the body of a certain page which is defined in the URL:
    For example:
    http://www.domain.com/print.php?page=index.htm
    HTML:
    Of course, the 'page=' variable would change from page to page...

    Here is the php I have so far:
    <?php
    ereg("^.*/",$SCRIPT_FILENAME,$tmp);
    $page_path = substr($tmp[0],0,-1);
    ?>
    <html>
    <head>
    	<base href="http://www.domain.com">
    	<meta name="robots" content="no index, no follow">
    	<title>Printer Friendly Page</title>
    </head>
    
    <body>
    <?php
    	// check if the filename for the page exists
    	if (!file_exists($_GET["page"]))
    	{
    	echo "<strong>We're sorry... we could not find that page</strong>";
    	}
    	else
    	{
    	// get the page content and place in a string	
    	$fcontents = join("", file($_GET["page"]));
    	$split = explode("<body>", $fcontents);
    	$split2 = explode("</body>", $split[1]);
    	$fcontents = $split2[0];
    	
    	// convert color attribute to ignore and bgcolor to bgignore
    	// this silently disables all color
    	$fcontents = ereg_replace("color","ignore",$fcontents);
    
    	// remove the target to a blank window attribute in links
    	$fcontents = ereg_replace("target=\"_blank\"","",$fcontents);
    
    	// replace the link end element with a single character marker
    	$fcontents = ereg_replace("</a>","¬",$fcontents);
    
    	// if any markers left restore link end element
    	$fcontents = ereg_replace("¬","</a>",$fcontents);
    
    	// finally print the page
    	echo $fcontents;
    	}
    ?>
    </body>
    </html>
    PHP:
    However, print.php just appears blank, and I don't know why. If anyone can spot an obvious (or not so obvious) error, that would be great.

    Thanks,
    SWagner
     
    swagner, Oct 14, 2007 IP
  2. ste.richards

    ste.richards Guest

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try putting error_reporting(E_ALL) at the top of your script and run again. If that fails try running 'php -l <script name>.php' at a shell if you have access.
     
    ste.richards, Oct 15, 2007 IP
  3. swagner

    swagner Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Which script do you mean? The one before the <html> tag, or the one inside the <body>? Also, is 'error_reporting(E_ALL)' all I need, or is there another function that is needed?
     
    swagner, Oct 15, 2007 IP
  4. le_gber

    le_gber Peon

    Messages:
    28
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if all you want to create is a printer friendly page, why not use the css media="print" attribute?
     
    le_gber, Oct 15, 2007 IP
  5. swagner

    swagner Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I want the user to see what the printer version looks like. I have tried the css media="print" attribute before, but people don't like printing such a colorful page because of the amount of ink it will take. They don't know that it will print other than what they see. They need something they can look at -- the print.php page...

    Also, can anyone help me out with this:
    $fcontents = file_get_contents($_GET['page']);
    
    $split = explode('<body>', $fcontents);
    
    $split2 = explode('</body>', $split[0]);
    
    $fcontents = $split2[0]; 
    PHP:
    It is supposed to take the contents of just the <body> tag, but it gives me the entire page code... what should I change?
     
    swagner, Oct 16, 2007 IP
  6. TechEvangelist

    TechEvangelist Guest

    Messages:
    919
    Likes Received:
    140
    Best Answers:
    0
    Trophy Points:
    133
    #6
    It looks $split2 is using the first cell from the $split array, which will be everything above the <body> tag. It doesn't include the content. The content would be in $split[1]. Also, this won't work if there are any attributes in the body tag.

    Try this:
    
    $fcontents = file_get_contents($_GET['page']);
    $split = explode('<body>', $fcontents);
    $split2 = explode('</body>', $split[1]);
    $fcontents = $split2[0];
    
    Code (markup):
     
    TechEvangelist, Oct 16, 2007 IP
  7. le_gber

    le_gber Peon

    Messages:
    28
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    A php stylesheet switcher might be a good solution if you are not set on your current path. Printer friendly page link loads the media='print' stylesheet
     
    le_gber, Oct 16, 2007 IP
  8. swagner

    swagner Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I tried that... It gave me this error/notice:
    Notice: Undefined offset: 1 in /opt/www/courses2-port80/wagnerlab/print.php on line 25
    Code (markup):
    Any suggestions?
     
    swagner, Oct 16, 2007 IP