如何在每个</头部>;在多个HTML页面站点中


How to add lines before each and every </head> in multiple HTML Page site?

我希望大家都做得很好。

如何在多个(在我的情况下有65个页面)html+PHP页面中关闭</head>之前添加respo_style.css

有什么最好的方法可以动态添加吗

<link rel="stylesheet" type="text/css" href="respo_style.css" />

使用代码?

在我的情况下,我有一个config.php,它在每一页中都被调用。

注意:不添加或更改任何其他文件。是否可以使用jQuery或JavaScript动态添加行。

简单的事情就是在现有文件中添加额外的文件,而不打开每个文件。

我相信有很多方法,但这里有一个关于最快/最简单实现IMO.的解释

对于重复的代码,应该将该代码存储在一个文件中,并使用PHP的include_once函数进行调用。但是,为了在文件中使用PHP,您需要在网页中使用.PHP扩展名而不是.HTML(假设您的文件是.HTML)。

这里有一个例子:

<head>
<?php include_once("repetitive_code_here.php"); ?> 
</head>

如果在脚本开始时运行ob_start();,则可以调用ob_get_contents()来获取页面并用样式表替换</head>

$string = ob_get_contents();
$link = '<link rel="stylesheet" type="text/css" href="respo_style.css" />';
echo str_replace('</head>', $link . '</head>', $string);
ob_end_clean();

或者将函数传递给ob_start(); 而不是ob_get_contents

function callback($buffer) {
    $link = '<link rel="stylesheet" type="text/css" href="respo_style.css" />';
    return str_replace('</head>', $link . '</head>', $buffer);
}
ob_start('callback');

但最后您需要调用ob_end_flush();

这将起作用。我在所有文件中都有style.css。所以我可以使用这个代码@import url("base.css");

respo_style.css包括在style.css

您应该这样做:

page.php(在所有65页中写下)

<?php
require_once('includes/head.php'); //Assuming you put head.php in a folder called "includes" inside the root
?>
//Then the content of the page

和head.php

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="respo_style.css" />
    </head>