Home php教程 PHP源码 php生成静态html页面方法

php生成静态html页面方法

Jun 08, 2016 pm 05:29 PM
content name parser position quot

下面讲的是用php生成静态html页面方法哦,方法有几种,各位朋友可以根据自己的实际情况来实现。

<script>ec(2);</script>

第一种:利用模板。目前PHP的模板可以说是很多了,有功能强大的smarty,还有简单易用的smarttemplate等。它们每一种模板,都有一个获取输出内容的函数。我们生成静态页面的方法,就是利用了这个函数。用这个方法的优点是,代码比较清晰,可读性好。
这里我用smarty做例子,说明如何生成静态页
require('smarty/Smarty.class.php');
$t = new Smarty;
$t->assign("title","Hello World!");
$content = $t->fetch("templates/index.htm");
//这里的 fetch() 就是获取输出内容的函数,现在$content变量里面,就是要显示的内容了
$fp = fopen("archives/2005/05/19/0001.html", "w");
fwrite($fp, $content);
fclose($fp);
?>
第二种方法:利用ob系列的函数。这里用到的函数主要是 ob_start(), ob_end_flush(), ob_get_content(),其中ob_start()是打开浏览器缓冲区的意思,打开缓冲后,所有来自PHP程序的非文件头信息均不会发送,而是保存在内部缓冲区,直到你使用了ob_end_flush().而这里最重要的一个函数,就是ob_get_contents(),这个函数的作用是获取缓冲区的内容,相当于上面的那个fetch(),道理一样的。代码:
ob_start();
echo "Hello World!";
$content = ob_get_contents();//取得php页面输出的全部内容
$fp = fopen("archives/2005/05/19/0001.html", "w");
fwrite($fp, $content);
fclose($fp);
?>
我选用的第2种方法 也就是用ob系列的函数
我刚开始看这个的时候有点不太明白 后来才知道ob是output buffering的意思 也就是输出缓存
当你准备输出的时候 所有的数据都保存在ob里面 服务器解析完php以后 把所有要输出到客户端的html代码都存放在ob里面 如果我们要输出html静态页面 只要把缓存取出来写入一个html页面即可
所以原理其实是很简单的
这里用到了几个函数 由于我初学php 很多函数我还不了解 所以这里也说明一下 希望可以帮助大家
ob_start():开始“捕捉”缓存 也就是从这里开始 打开浏览器的缓存
ob_end_flush():关闭浏览器缓存
ob_get_content():读取缓存内容
fopen("文件路径","打开模式")打开文件 97xxoo这个函数的打开模式有好几种 下面介绍几种主要的模式:
'r' 只读方式打开,将文件指针指向文件头。??
'r+' 读写方式打开,将文件指针指向文件头。??
'w' 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。??
'w+' 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。??
fwrite("文件名称","写入内容") 写入文件
fclose() 关闭文件
由于我要转换的html文件非常多 可能有几百个 所以这里不能静态指定fopen的路径 大家可以设置一个路径变量 里面可以保存用户传来的id等信息 方便进行html文件命名 下面是我结合上次php读取xml数据的一个简单例子 程序代码
ob_start();????????????//打开浏览器缓存
?>
//下面是读取xml数据
$parser = xml_parser_create(); //创建一个parser编辑器
xml_set_element_handler($parser, "startElement", "endElement");//设立标签触发时的相应函数 这里分别为startElement和endElenment
xml_set_character_data_handler($parser, "characterData");//设立数据读取时的相应函数
$xml_file="1.xml";//指定所97gan要读取的xml文件,可以是url
$filehandler = fopen($xml_file, "r");//打开文件
while ($data = fread($filehandler, 4096))
{
xml_parse($parser, $data, feof($filehandler));
}//每次取出4096个字节进行处理
fclose($filehandler);
xml_parser_free($parser);//关闭和释放parser解析器
$name=false;
$position=false;
function startElement($parser_instance, $element_name, $attrs) //起始标签事件的函数
{
global $name,$position;??
if($element_name=="NAME")
{
$name=true;
$position=false;
echo "名字:";
}
if($element_name=="POSITION")
{$name=false;
$position=true;
echo "职位:";
}
}
function characterData($parser_instance, $xml_data) //读取数据时的函数
//此教程来源于97xxoo教程网(www.97xxoo.org)
查看完整的教程请点:http://www.97xxoo.org/article/1/2008/20081123423.shtml
{
??global $name,$position;
??if($position)
echo $xml_data."
";
if($name)
echo $xml_data."
";
}
function endElement($parser_instance, $element_name) //结束标签事件的函数
{
global $name,$position;
$name=false;
$position=false;
}
?>
//xml数据读取完毕
$htmlname=$id.".html";??????????????????????????//$id可以自己定义 这里代表用户传来的id
$htmlpath="archives/".$htmlname;?????? //设置路径变量
$content = ob_get_contents();??????????????//取得php页面输出的全部内容
$fp = fopen($htmlpath, "w");
fwrite($fp, $content);
fclose($fp);
?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement recursive descent Parser in Python How to implement recursive descent Parser in Python May 17, 2023 am 08:44 AM

1. Arithmetic expression evaluation To parse this type of text, another specific grammar rule is required. We introduce here the grammatical rules Backus Normal Form (BNF) and Extended Backus Normal Form (EBNF) that can represent context-free grammars. In fact, everything from an arithmetic expression to almost all programming languages ​​are defined by context-free grammars. For simple arithmetic expressions, assume that we have used word segmentation technology to convert it into an input tokens stream, such as NUM+NUM*NUM (see the previous blog post for word segmentation methods). On this basis, we define the BNF rule as follows: expr::=expr+term|ex

CSS layout property optimization tips: position sticky and flexbox CSS layout property optimization tips: position sticky and flexbox Oct 20, 2023 pm 03:15 PM

CSS layout attribute optimization tips: positionsticky and flexbox In web development, layout is a very important aspect. A good layout structure can improve the user experience and make the page more beautiful and easy to navigate. CSS layout properties are the key to achieving this goal. In this article, I will introduce two commonly used CSS layout property optimization techniques: positionsticky and flexbox, and provide specific code examples. 1. positions

Flexible application skills of position attribute in H5 Flexible application skills of position attribute in H5 Dec 27, 2023 pm 01:05 PM

How to flexibly use the position attribute in H5. In H5 development, the positioning and layout of elements are often involved. At this time, the CSS position property will come into play. The position attribute can control the positioning of elements on the page, including relative positioning, absolute positioning, fixed positioning and sticky positioning. This article will introduce in detail how to flexibly use the position attribute in H5 development.

How to put div at the bottom in html How to put div at the bottom in html Mar 02, 2021 pm 05:44 PM

How to place a div at the bottom of HTML: 1. Use the position attribute to position the div tag relative to the browser window, with the syntax "div{position:fixed;}"; 2. Set the distance to the bottom to 0 to permanently place the div at At the bottom of the page, the syntax is "div{bottom:0;}".

How to use position in h5 How to use position in h5 Dec 26, 2023 pm 01:39 PM

In H5, you can use the position attribute to control the positioning of elements through CSS: 1. Relative positioning, the syntax is "style="position: relative;"; 2. Absolute positioning, the syntax is "style="position: absolute;" "; 3. Fixed positioning, the syntax is "style="position: fixed;" and so on.

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

What attributes does position have? What attributes does position have? Oct 10, 2023 am 11:18 AM

The position attribute values ​​include static, relative, absolute, fixed, sticky, etc. Detailed introduction: 1. static is the default value of the position attribute, which means that the elements are laid out according to the normal document flow without special positioning. The position of the elements is determined by their order in the HTML document and cannot be passed through top, right, and bottom. Adjust with the left attribute; 2. relative is relative positioning and so on.

How to clear position in css How to clear position in css Oct 07, 2023 pm 12:02 PM

How to clear position in css: 1. Use the static attribute, which can be set to static to clear the position attribute; 2. Use the inherit attribute to clear the position attribute of the element and inherit the position attribute of the parent element; 3. Use the unset attribute, Restore the attributes to their default values ​​and clear the position attribute of the element; 4. Use the !important rule, which will override other style rules and clear the position attribute, etc.

See all articles