合肥PHP培训中心(php批量生成html文件)

合肥PHP培训中心:php世界上最好的编程语言,^_^,本文只是记录php批量生成html文件的php代码,仅供参考。至于用途,只能说很大,但是对于SEO而言,这种应该算是灰帽SEO手法了。今天,小小课堂网( www.xxkt.org )分享的内容为《合肥PHP培训中心(php批量生成html文件)》。希望对大家有所帮助。

合肥PHP培训中心

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

1、链接数据库php代码

代码中mysql_host、mysql_user和mysql_password分别对应数据库名、数据库用户名、数据库密码。my_database对应sql表的名字。

<?php
$link = mysql_connect(“mysql_host” , “mysql_user” , “mysql_password” )or die(“Could not connect : ” . mysql_error());
mysql_query(“set names utf8”);
mysql_select_db(“my_database”) or die(“Could not select database”);
?>

2、批量生成html的php代码

选择my_table表中的id、title、introduce,也就是创造表的时候得有这些。

<?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=”html/$id.html”;
$fp=fopen(“template.html”,”r”); //只读打开模板
$str=fread($fp,filesize(“template.html”));//读取模板中内容
$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=html/$id.html>生成成功</a>”.”<br>”;
}

/* 释放资源 */
mysql_free_result($result);
mysql_close($link);
?>

3、php生成用到的template.html代码

这里的{introduce}就是调用的内容。

<!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/html; charset=utf-8″ />
<title>{title}</title>
</head>

<body>
{introduce}
</body>
</html>

以上就是小小课堂网( www.xxkt.org )分享的内容为《合肥PHP培训中心(php批量生成html文件)》。感谢您的阅读。

所有文章均为小小课堂网原创。发布者:SEO免费培训教程,转转请注明出处:https://www.xxkt.org/14983

(0)
上一篇 2022年2月24日 上午9:11
下一篇 2022年2月24日 上午9:26

相关推荐

发表评论

您的电子邮箱地址不会被公开。

error: Content is protected !!