使用PHP(和加载样式)显示HTML内容

问题描述 投票:0回答:2

我有一些HTML页面(在“web”文件夹中)使用一些样式和脚本,我需要使用PHP脚本显示这些HTML页面。这是我正在使用的代码的简化版本:

<?php

 readfile("web/index.html");

它可以工作,但它不加载任何资源(css,js,images),因为URL不是绝对的:

 <link href="css/bootstrap.css" rel='stylesheet' type='text/css' />
 <link href="css/style.css" rel='stylesheet' type='text/css' />

它找不到资源,因为它在与PHP脚本相同的文件夹中查找css文件夹。

有什么解决方法吗?

php html .htaccess http-status-code-404
2个回答
2
投票

你需要在html中添加添加<base href='...'>,就像这样(未经测试):

$page_content=preg_replace("%</head%i","<base href='web/'></head",$page_content);

在readfile之后插入它。见MDN web docs for more information about that tag

在某些浏览器(至少是Firefox和边缘)中,如果在输出$ page_content之前打印基本标记,它也可以工作。它很脏,但也许它足够好了。


0
投票

很简单,你可以使用这样的东西

的index.php

<html>
 <head>
  <php include("menu.php"); ?>
 </head>
<body>

</body>
</html>

menu.php

<link href="css/bootstrap.css" rel='stylesheet' type='text/css' />
<link href="css/style.css" rel='stylesheet' type='text/css' />

对于基本路径使用一些变量,如$base = "http://localhost"然后使用您的元素,如<img scr="<?php $base ?>/images/myimage.jpg" />

希望这能回答你的问题

© www.soinside.com 2019 - 2024. All rights reserved.