zoukankan      html  css  js  c++  java
  • PHP基础-生成静态html页面原理是怎样

    设置example.html为模板文件,然后按照此模板文件生成article-1.html~article-5.html,以此来做简单的演示,代码如下:

    <?php//将数据存入二维数组$con=array(array('文章标题1','文章内容1'),array('文章标题2','文章内容2'),array('文章标题3','文章内容3'),array('文章标题4','文章内容4'),array('文章标题5','文章内容5'));foreach($con as $id=>$val){ //循环生成

    $title=$val[0]; $content=$val[1]; $path="article-".($id+1).".html"; //替换example内容,并获取内容赋值给$str

    $fp=fopen("example.html","r"); $str=fread($fp,filesize("example.html")); $str=str_replace("{title}",$title,$str); $str=str_replace("{content}",$content,$str);

    fclose($fp); //新建空白文件,将$str写入

    $handle=fopen($path,"w");

    fwrite($handle,$str);

    fclose($handle); echo "生成".$path."<br/>";

    }?>}

    注解:

    fopen(文件名,打开方式),打开文件函数,若无文件,则创建。其返回值为资源型;

    fread(文件名,读取字节数),读取文件内容及对应的字节数;

    str_replace(规定要查找的值,替换被查找值的值,被搜索的字符串),替换函数;

    fclose(文件名),关闭文件;

    fwrite(要写入的打开文件,要写入打开文件的字符串,要写入的最大字节数)。

    Andreas Creten explains his view on wether you should always try to write perfect code. Spoiler: no.

    The engineers want to write perfect code using the latest techniques, make sure that the code is well documented so they can fully understand how everything works and that it has tests so they can easily update things later. Product owners on the other hand just want things to be done, fast and cheap, so they can ship new features or convince new clients.

    How can you make these conflicting views work together?

    https://medium.com/we-are-madewithlove/does-code-need-to-be-perfect-a53f36ad7163

    Freek Van der Herten

    Freek Van der Herten is a partner and developer at Spatie , an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modernphp and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band .

  • 相关阅读:
    电路中的VCC和VDD
    动态数码管 什么是位选 段选
    pytest文档62-内置fixture之request
    pytest文档61-fixture之name参数使用别名
    pytest文档60-pytest.main()的使用
    python笔记49-yaml文件中变量的使用(锚点& 与 引用*)
    python笔记48-面试题:m1={'a':1,'b':2,'c':1} 将同样的value的key集合在list里,输出{1:['a','c'],2:['b']}
    jenkins学习15-Allure报告不用登陆也能给领导看
    python接口自动化35-pyppeteer-install下载没反应,r.html.render() 下载无反应问题解决
    kvm内存优化--KSM
  • 原文地址:https://www.cnblogs.com/2881064178dinfeng/p/6145834.html
Copyright © 2011-2022 走看看