>백엔드 개발 >PHP 튜토리얼 >php打印网页的代码举例来说

php打印网页的代码举例来说

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB원래의
2016-06-13 11:26:091771검색

php打印网页的代码举例
本文出处参考:http://www.jbxue.com/article/6793.html
为大家介绍一个可以生成方便打印的页面的php代码,很多新闻网站都有这个功能,生成的页面简洁大方,去掉了部分多余的元素,整个页面只包括标题、发布时间、内容等主要元素。这个不同于window.print()的功能哦。

具体实现代码如下:

//从环境变量中得到文件的相对路径$page=substr($SCRIPT_NAME,1);// 显示一个图标并连接到打印页// 便于打印页面的生成程序pfp.php?><a href="pfp.php?page=<?=%24page?>">;<img src="printer.gif"    style="max-width:90%"  style="max-width:90%" border="0" alt="点我跳转打印页面">打印本页</a>


把当前页面的名称传递到pfp.php程序中,这个程序使用PHP的“file”函数把页面作为一个字符串来处理。当这个页面被载入的时候,程序就可以增加、改写或删除HTML片段。
<?ereg ('^.*/',$SCRIPT_FILENAME,$tmp);$page_path = substr($tmp[0],0,-1);?><base href="http://<?%20echo%20%24HTTP_HOST%20?>/"><meta name="robots" content="no index, no follow"><title>Printer Friendly Page</title>
// check if the filename for the page existsif (!file_exists("$page.inc")){echo "Error - The page =$page?>"."does not exist on this site.";}else{// 得到页面的内容并把它放到一个字符串中$fcontents = join('', file("$page.inc"));// 忽略颜色属性,转换以'ignore'替代'color'$fcontents = ereg_replace('color','ignore',$fcontents);// 去除超链接中的 “_blank”$fcontents = ereg_replace('target=\"_blank\"','',$fcontents);// 替换标记$fcontents = ereg_replace('','',$fcontents);// 显示URL的绝对地址$fcontents = ereg_replace(']*>;([^]*)','\\2(\\1)',$fcontents);// 把相对链接转为绝对链接$fcontents = ereg_replace(']*>([^]*)',"\\2(http://$HTTP_HOST/\\1)";,$fcontents);// 背景颜色改回白色$fcontents = ereg_replace('',$fcontents);// 输出页面echo $fcontents;}?>

include("$page_path/footer.inc"); ?>

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.