php 批量生成html,txt文件的实现代码

php 批量生成html,txt文件的实现代码,第1张

首先建立一个conn php的文件用来链接数据库

复制代码 代码如下: <?php $link = mysql_connect("mysql_host" "mysql_user" "mysql_password" )or die("Could not connect : " mysql_error())mysql_query("set names utf ")mysql_select_db("my_database") or die("Could not select database")?>

php 批量生成

复制代码 代码如下: <?php require_once(“conn php”) $query = "SELECT id title introduce FROM my_table"$result = mysql_query($query) or die("Query failed : " mysql_error())/* 生成 HTML 结果 */ while ($row = mysql_fetch_array($result MYSQL_ASSOC)) { $id=$row[ id ]$title=$row[ title ]$introduce=$row[ introduce ]$path="/$id "$fp=fopen("template " "r")//只读打开模板 $str=fread($fp filesize("template "))//读取模板中内容 $str=str_replace("{title}" $title $str)$str=str_replace("{introduce}" $introduce $str)//替换内容 fclose($fp)$handle=fopen($path "w")//写入方式打开新闻路径 fwrite($handle strip_tags($introduce))//把刚才替换的内容写进生成的HTML文件 fclose($handle)//echo "<a href=/$id >生成成功</a>" "<br>"} /* 释放资源 */ mysql_free_result($result)mysql_close($link)?>

template 文件内容

复制代码 代码如下: <!DOCTYPE PUBLIC " //W C//DTD XHTML Transitional//EN" " <xmlns=" <head><meta equiv="Content Type" content="text/charset=utf " /><title>{title}</title></head><body>{introduce} </body></>

php 批量生成txt

复制代码 代码如下: lishixinzhi/Article/program/PHP/201311/20863

可以使用htmlentities函数

<?php 

header("content-type:text/htmlcharset=utf-8")

$str = '<a href="http://www.baidu.com">百度一下</a>'

echo htmlentities($str)

 ?>

输出结果

<a href="http://www.baidu.com">百度一下</a>

其实很简单,只要把html语句中的双引号改为单引号,然后直接用echo输出就行了:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>无标题文档</title>

</head>

<body>

<ul class="nav navbar-nav navbar-right">

<li class="nli"><a href="/About/">关于我们</a></li>

<li class="nli"><a href="/Notice/">舞蹈</a></li>

<li class="nli"><a href="/Instructors/">师资阵容</a></li>

</li></ul>

<?php

echo("<ul class='nav navbar-nav navbar-right'>")

echo("<li class='nli'><a href='/About/'>关于我们</a></li>")

echo(" <li class='nli'><a href='/Notice/'>舞蹈</a></li>")

echo(" <li class='nli'><a href='/Instructors/'>师资阵容</a></li>")

echo("</li></ul>")

?>

</body>

</html>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/6154113.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-16
下一篇 2023-03-16

发表评论

登录后才能评论

评论列表(0条)

保存