php打印网页的代码举例
本文出处参考:http://www.jbxue.com/article/6793.html
为大家介绍一个可以生成方便打印的页面的php代码,很多新闻网站都有这个功能,生成的页面简洁大方,去掉了部分多余的元素,整个页面只包括标题、发布时间、内容等主要元素。这个不同于window.print()的功能哦。
具体实现代码如下:
<?//从环境变量中得到文件的相对路径$page=substr($SCRIPT_NAME,1);// 显示一个图标并连接到打印页// 便于打印页面的生成程序pfp.php?><a href="pfp.php?page=<?=$page?>">;<img src="printer.gif" style="max-width:90%" style="max-width:90%" border="0" alt="点我跳转打印页面">打印本页</a>
把当前页面的名称传递到pfp.php程序中,这个程序使用php的“file”函数把页面作为一个字符串来处理。当这个页面被载入的时候,程序就可以增加、改写或删除html片段。
软件介绍:金戈企业建站系统不仅是一份免费的企业建站代码包,而且它还是完全开源的,它倾注了作者1个多月来日日夜夜的心血,虽然有些地方没做到尽善尽美,可我相信在接下来的日子里我会通过反馈信息让她更丰满实用起来。1.完美的摸板机制,即使你对php一点也不懂,只要你会做网页。就可以立即打造新颖别致的网站界面(摸板制作方法手册正在紧张制作中,稍后发布)可惜作者精力有限,目前只提供一套摸板。不过只是暂时的2.
<?ereg('^.*/',$SCRIPT_FILENAME,$tmp);$page_path = substr($tmp[0],0,-1);?><html><head><base href="http://<? echo $HTTP_HOST ?>/"><meta name="robots" content="no index, no follow"><title>Printer Friendly Page</title></head><body bgcolor="white"><table border="0" cellpadding="5" cellspacing="0" width="630" ><tr><td valign="top"><?// check if the filename for the page existsif (!file_exists("$page.inc")){echo "<strong>Error - The page <?=$page?>"."does not exist on this site.</strong>";}else{// 得到页面的内容并把它放到一个字符串中$fcontents = join('', file("$page.inc"));// 忽略颜色属性,转换以'ignore'替代'color'$fcontents = ereg_replace('color','ignore',$fcontents);// 去除超链接中的 “_blank”$fcontents = ereg_replace('target="_blank"','',$fcontents);// 替换</a>标记$fcontents = ereg_replace('</a>','',$fcontents);// 显示URL的绝对地址$fcontents = ereg_replace('<a[^h]*href="(http://[^"]*)"[^>]*>;([^]*)','<strong>\2</strong><em>(\1)</em>',$fcontents);// 把相对链接转为绝对链接$fcontents = ereg_replace('<a[^h]*href="([^"]*)"[^>]*>([^]*)',"<strong>\2</strong><em>(http://$HTTP_HOST/\1)</em>";,$fcontents);// 背景颜色改回白色$fcontents = ereg_replace('<body bgignore','<body bgcolor', $fcontents);// if any markers left restore link end element$fcontents = ereg_replace('','</a>',$fcontents);// 输出页面echo $fcontents;}?></td></tr><tr><td align="center"><hr width="90%"></td></tr><tr><td align="center"><? include("$page_path/footer.inc"); ?></td></tr></table></body></html>










