Heim >Backend-Entwicklung >PHP-Tutorial >php鐢熸垚闈欐€侀〉闈唬鐮乢PHP鏁欑▼ | 甯涔嬪
php鐢熸垚闈欐€侀〉闈唬鐮佹湰娆剧敓鎴愰潤鎬侀〉闈㈢▼搴忓疄鐜板師鐞嗘槸鍋氬ソ鑷畾鐨勬ā鏉挎爣绛撅紝鐒跺悗鐢眘tr_replace鎶婃爣绛炬浛鎹㈡垚鎸囧畾鐨勫唴瀹癸紝鍐嶇敱fopen鐢熸垚鎸囧畾 鏂囦欢鍚嶇殑闈欐€侀〉闈紝杩欐牱灏監K浜嗐€?p>php鏁欑▼鐢熸垚闈欐€侀〉闈唬鐮?br />鏈鐢熸垚闈欐€侀〉闈㈢▼搴忓疄鐜板師鐞嗘槸鍋氬ソ鑷畾鐨勬ā鏉挎爣绛撅紝鐒跺悗鐢眘tr_replace鎶婃爣绛炬浛鎹㈡垚鎸囧畾鐨勫唴瀹癸紝鍐嶇敱fopen鐢熸垚鎸囧畾 鏂囦欢鍚嶇殑闈欐€侀〉闈紝杩欐牱灏眔k浜嗐€?/p>
header('content-type:text/html;charset=utf-8');
if(!function_exists('file_get_contents')){ //濡傛灉绯荤粺娌℃湁file_get_contents()鍑芥暟
function file_get_contents($file){ //鑷繁鍐檉ile_get_contents()鍑芥暟
$fp = fopen($file,'r');
$content = fread($fp,filesize($file));
fclose($fp);
return $content;
}
}
$tmp_file = 'template.html'; //妯℃澘鏂囦欢
$content = file_get_contents($tmp_file); //鑾峰緱妯℃澘鏂囦欢鍐呭
$title = "title"; //妯℃澘鍙橀噺title瑕佹浛鎹㈢殑鍊?br />$text = 'text'; //妯℃澘鍙橀噺text瑕佹浛鎹㈢殑鍊?br />$content = str_replace('',$title,$content); //鏇挎崲妯℃澘鍙橀噺title
$content = str_replace('',$text,$content); //鏇挎崲妯℃澘鍙橀噺text
//echo $content; //鏄剧ず鏇挎崲鍚庣殑妯℃澘鏂囦欢鍐呭
makehtml('news.html',$content);//鍐欏叆鐢熸垚鍚庣殑闈欐€佹枃浠跺唴瀹瑰埌news.html鏂囦欢
echo '鏌ョ湅鏂囦欢';
function makehtml($file,$content){
$fp = fopen($file,'w');
fwrite($fp,$content);
fclose($fp);
}
?>
//template.html
makehtml
杩欐槸妯℃澘鍙橀噺title------
杩欐槸妯℃澘鍙橀噺text------