我需要读取一个字符串,检测 {VAR},然后执行 file_get_contents('VAR.php') 代替 {VAR}。“VAR”可以命名为任何名称,例如 TEST 或 CONTACT-FORM 等。我不想知道 VAR 是什么——不是执行硬编码条件,而是只是看到一个用大括号括起来的大写字母数字标签,然后执行 file_get_contents() 来加载它。

我知道我需要使用 preg_match 和 preg_replace,但我在 RegExp 上遇到了困难。

这有什么用?它对于挂钩 WordPress 很有用。

有帮助吗?

解决方案

以上猎户座有一个正确的解决方案,但它是不是真的有必要在简单情况下使用一个回调函数。

假设文件名是A-Z +连字符可以使用PHP的/ E标志在正则表达式做,在1行:

$str = preg_replace('/{([-A-Z]+)}/e', 'file_get_contents(\'$1.html\')', $str);

此将与VAR.html的内容替换{VAR}的任何实例。如果你需要指定一个特定的目录,你可以在它前面加上路径进入第二个任期。

有作为上述相同的含糊安全无忧,但我想不出任何具体的。

其他提示

您需要做很多事情。我假设您可以进行跑腿工作以将要预处理的页面数据获取为字符串。

  1. 首先,您需要正则表达式才能正确匹配。这应该相当容易,比如 /{\w+}/.

  2. 接下来,您需要使用 preg_match 的所有标志来获取页面数据中的偏移位置。通过此偏移量,您可以将字符串分为匹配的前部分、匹配部分和后部分。

  3. 一旦你有了这 3 个部分,你需要运行你的包含,并将它们重新粘在一起。

  4. 起泡沫,冲洗,重复。

  5. 当你发现不再有变量时停止。

这并不是非常有效,并且可能有更好的方法。您可能希望考虑执行 preg_split 来代替,拆分 /[{}]/. 。无论您如何分割它,您都假设您可以信任传入的数据,这将大大简化整个过程。为此,我将像这样布置代码:

  1. 获取您的内容并将其拆分,如下所示: $parts = preg_split('/[{}]/', $page_string);

  2. 根据以下条件编写一个递归函数:

    • 当 arg 长度 < 3 时停止
    • 否则,返回一个由以下组成的新数组
    • $arg[0] 。load_data($arg[1]) 。$arg[2]
    • 加上 $argv[3...] 中剩下的内容
  3. 通过 $parts 运行您的函数。

您可以不用正则表达式(但愿),是这样的:

//return true if $str ends with $sub
function endsWith($str,$sub) {
    return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
}

$theStringWithVars = "blah.php cool.php awesome.php";
$sub = '.php';
$splitStr = split(" ", $theStringWithVars);
for($i=0;$i<count($splitStr);$i++) {
    if(endsWith(trim($splitStr[$i]),$sub)) {
        //file_get_contents($splitStr[$i]) etc...
    }    
}

从我的脑海中浮现出来,你想要这个:

// load the "template" file
$input = file_get_contents($template_file_name);

// define a callback. Each time the regex matches something, it will call this function.
// whatever this function returns will be inserted as the replacement
function replaceCallback($matches){
  // match zero will be the entire match - eg {FOO}. 
  // match 1 will be just the bits inside the curly braces because of the grouping parens in the regex - eg FOO
  // convert it to lowercase and append ".html", so you're loading foo.html

  // then return the contents of that file.
  // BEWARE. GIANT MASSIVE SECURITY HOLES ABOUND. DO NOT DO THIS
  return file_get_contents( strtolower($matches[1]) . ".html" );
};
// run the actual replace method giving it our pattern, the callback, and the input file contents
$output = preg_replace_callback("\{([-A-Z]+)\}", replaceCallback, $input);

// todo: print the output

现在我将解释正则表达式

 \{([-A-Z]+)\}
  • \{\} 只需告诉它匹配花括号即可。你需要斜杠,如 {} 是特殊字符,因此需要转义。
  • () 创建一个分组。基本上,这可以让您提取比赛的特定部分。我在上面的函数中使用它来仅匹配大括号内的内容,而不匹配大括号本身。如果我不这样做,那么我需要剥离 {} 退出比赛,这会很烦人
  • [-A-Z] 说“匹配任何大写字符,或者 -
  • + 之后 [-A-Z] 意味着我们需要至少有 1 个字符,但最多可以有任意数量。

相对来说,正则表达式是昂贵的。虽然你可能需要用它们来找出哪些文件来加载,你肯定不需要他们做了更换,而且可能不应该使用正则表达式。毕竟,你知道你要更换什么,为什么你需要模糊搜索?

使用关联数组和str_replace函数做你的替代品。 str_replace函数支持同时做多个取代阵列。一个线取代,没有循环。

例如:

$substitutions = array('{VAR}'=>file_get_contents('VAR.php'),
'{TEST}'=>file_get_contents('TEST.php'),
...
);

$outputContents = str_replace( array_keys($substitutions), $substitutions, $outputContents);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top