【问题标题】:How to run shtml include in php如何在php中运行shtml包含
【发布时间】:2011-06-30 16:01:08
【问题描述】:

我在服务器上有几十个 .shtml 文件,其中包含此语句以包含 .inc 文件:

 <!--#include virtual="../v4/rightmenu.inc" -->

这正是它在运行良好的源上的显示方式。

我想知道是否可以在不更改的情况下在我的 php 代码上运行它,因为当前文件有很多这种包含,我不想弄乱很多这样的代码。

我只是不想把它改成&lt;?php include "../v4/rightmenu.inc"; ?&gt;之类的东西

【问题讨论】:

  • SHTML 在当时很不错,但您基本上是在努力保持石器时代的活力。进入 90 年代......我们有饼干。
  • 这就是你投反对票的原因?如果可以的话,我可以,但我正在处理数百页,我不想花费我的一生来改变它们。
  • 您可以编写一个重写规则 (mod_rewrite/htaccess) 以通过一个 php 文件传递​​所有这些 .shtml 文件,该文件又会在请求的文件中查找/解析 #include 语句?!跨度>
  • @Yoshi:我没听懂,你能详细说明你的答案吗?
  • 我没有投反对票。当我发表评论时,它已经在那里了。我只对明显完全错误的事情投反对票。您对此有不同的看法,这不是“错误的”。但同样,现在忍受一点短期的痛苦并修复你的文件,然后你以后就不必再处理这个问题了。

标签: php shtml


【解决方案1】:

使用mod_rewrite 通过单个php 文件将所有请求路由到.shtml 文件。例如:_includeParser.php

此类文件的粗略草图可能是:

/**
 * This function should return the contents of the included file
 */
function getIncludeFileContents($match) {
    list (, $file) = $match;
    // return something for $file
}

/**
 * This function should return a php-readable path for the initially requested .shtml file
 */
function getRealpath($uri) {
  // return the real path of the requested uri
}

// Map the requested uri to file
// 'REDIRECT_URL' could be something different for your configuration, have a look at the $_SERVER array
// to get the correct value if something fails here
$realpath = getRealpath($_SERVER['REDIRECT_URL']); 

// parse each include statement
// the regex pattern here could need some tweaking
$output = preg_replace_callback(
  '@<!--#include virtual="(.+?)" -->@i',
  'getIncludeFileContents',
  file_get_contents($realpath)
);

// output the final contents
echo $output;

当然,这样的解决方案可以通过一些缓存或其他方法进行优化。

【讨论】:

  • 在每次请求时动态替换有什么意义?
  • @Krab 与包含 php 文件一样。可重用性。出于什么其他原因,您会将代码/文本块放在不同的文件中?
  • 不,我的意思是用&lt;?php include&lt;? echo file_get_contents 一次性替换#include 指令有什么好处?
  • 如果 preg 是贪婪的(我认为它是贪婪的),如果在#include 之后还有另一个以" --&gt; 结尾的 HTML 注释,那么您的正则表达式将不起作用。也许另一个#include。你的意思不是.+,而是[^"]+
  • @Kran 啊!当然这是一种可能。问题是,需要将服务器配置为将 .shtml 文件传递​​给 php 解释器。关于正则表达式,我并不是说这个例子可以按原样使用。我只是想勾勒出这个想法。
猜你喜欢
  • 2010-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 2018-05-09
  • 2016-01-06
  • 1970-01-01
  • 2014-02-15
相关资源
最近更新 更多