php 模板替换实现静态页面简单代码实现

1.处理页面

<?php
header("content-type:text/html;charset=utf8");
//连接数据库    
$pdo=new PDO("mysql:host=localhost;dbname=exam",'root','root');
$pdo->exec("set names utf8");
if ($_POST) {
    $title=$_POST['title'];
    $content=$_POST['content'];
    $arr=$pdo->exec("insert into newss(title,content) values('$title','$content')");
    //获得模版文件  网页的形式
    $str=file_get_contents("mo.htm");
    //echo $str;
    //替换内容
    $str=str_replace('{$title}',$title,$str);
    $str=str_replace('{$content}',$content,$str);
    //生成静态页面
    $id=$pdo->lastInsertId();
    $filename=$id.".html";
    file_put_contents('momo/'.$filename,$str);
} else {
    include "a.html";
}

?>

访问模板页面

<?php
header("content-type:text/html;charset=utf8");
//连接数据库    
$pdo=new PDO("mysql:host=localhost;dbname=exam",'root','root');
$pdo->exec("set names utf8");
$arr=$pdo->query("select * from newss")->fetchAll(PDO::FETCH_ASSOC);
?>
<table border="1">
    <tr>
        <td>编号</td>
        <td>标题</td>
    </tr>
    <?php foreach ($arr as $ke => $v) { ?>
    <tr>
        <td><?php echo  $v['id']  ?></td>
        <td><a href="momo/<?php echo $v['id'] ?>.html"><?php echo  $v['title']  ?></a></td>
    </tr>
    <?php  }  ?>
</table>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值