Home > Article > Backend Development > PHP fopen and fwrite functions create html pages_PHP tutorial
Ideas
Use the fopen function and the fread function to get the template, then use the str_replace function to replace the template tag with a variable, and finally use the fwrite function to output the new HTML page
index.html template page
header('Content-Type:text/html; charset=utf-8'); $conn=mysql_connect('localhost','root',''); $db=mysql_select_db('bbs',$conn ); mysql_query('set names utf8'); $sql="select * from notice"; $query=mysql_query($sql); //print_r($arr); while($arr=mysql_fetch_array($query)) { $title=$arr[title]; ; $content=$arr[content]; $file="index.html"; $neirong=$arr[id].".html"; $fp=fopen($file,' r'); $ht=fread($fp,filesize($file)); $str=str_replace('{title}',$title,$ht); $str=str_replace('{content}',$content ,$str); fclose($file); $file=fopen($neirong,'w'); $write=fwrite($file,$str); } ?>
Author "PHP Study Notes"