【问题标题】:PHP replace include with html blockPHP 用 html 块替换 include
【发布时间】:2014-07-30 09:02:19
【问题描述】:

我正在尝试使用 PHP 解析 .php 文件,找到任何 <?php include $root.'whatever.html'; ?> 并替换为一段 html 代码。

到目前为止,我遇到了两个问题,如何将whatever.html 捕获到变量中,以及如何不将$root 变量视为本地引用。

【问题讨论】:

  • 使用 file_get_contents 将 html 捕获到变量中
  • 您可以从 Regex Tag Wiki 开始学习 regex:stackoverflow.com/tags/regex/info
  • $content = file_get_contents('whatever.html'); // 在这里用 $content 做某事。

标签: php regex pcre


【解决方案1】:

您可以使用以下正则表达式:

(<\?php\s*(?:include|require|include_once|require_once)\s*.*['"](.*)['"]\s*;*\s*\?>)

捕获整个包含文本和whatever.html,以便您可以轻松地将其替换为您的自定义代码(当然,在处理真实文件时,您应该在使用它之前创建备份,以防您想修改原始文件)。

Demo

【讨论】:

  • 谢谢,你怎么能把它变成一个 php 字符串而不用引号打破?
  • @frankpulsar 可能regex101.com/r/hG1jF5/5 应该可以工作。它不是在这个问题中没有详细解释的新问题
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 2015-01-23
  • 2013-01-30
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多