如何将代码添加到<;头></头>;在HTML和PHP中,然后显示它?

如何将代码添加到<;头></头>;在HTML和PHP中,然后显示它?,php,head,Php,Head,我想创建一个文件(index.php),它将动态地将下面的代码添加到另一个名为(index2.php)的文件的头部,然后用修改后的代码显示页面 <link rel="stylesheet" type="text/css" href="new.css"> <script src="new.js"></script> <script> new(); </script> 但是,我不知道如何将代码添加到index2.php的head部分 我该

我想创建一个文件(index.php),它将动态地将下面的代码添加到另一个名为(index2.php)的文件的头部,然后用修改后的代码显示页面

<link rel="stylesheet" type="text/css" href="new.css">
<script src="new.js"></script>
<script>
new();
</script>
但是,我不知道如何将代码添加到index2.php的head部分

我该怎么做呢?

也许只是:

index2.php:

。。。
...

将index2.php文件的头标记代码放在不同的文件中。然后将该php文件包含在index.php文件的标记中 喜欢 index.php 包括一次('siffrent_file_name.php');
..

经过深思熟虑之后,我觉得我不想解析整个HTML文档,所以我在index2.php中添加了一个“键”,并使用以下代码:

$file=“index2.php”;
$key=“”;
$appcode='link rel=“stylesheet”type=“text/css”href=“new.css”>
新的();

是的,这是一种方法,但是它要复杂得多,不能那样工作。
include 'index2.php';
$file = "index2.php";
$key = "<!-- KEY -->";
$appcode = 'link rel="stylesheet" type="text/css" href="new.css">
<script src="new.js"></script>
<script>
new();
</script';

$index = fopen($file, "r") or die("Unable to open index symlink");
$code= fread($index,filesize("index2.php"));
fclose($index);

$index_app = (preg_replace ($key, $appcode, $code));

echo($index_app);