我想知道如何使用 php 将 html 文档插入另一个文档。例如:
header.html
<div id="header">Hey this is header</div>
index.php
<body>
<? get_document('header.html'); ?>
...
我可以为此使用 ajax,但我不希望我的网站依赖于 js。谢谢!
<html>
...
<body>
<?php
include("header.html");
?>
</body>
</html>
或者,如果您想将其存储在变量中并稍后根据需要使用
ob_start();
include("header.html");
$content = ob_get_contents();
ob_end_clean();
echo $content;
您可以只使用include
/ require
。这可以在任何文件上完成,而不仅仅是 PHP 文件。
你也可以这样做echo file_get_contents('header.html');
,但include
在这种情况下更有意义。