正则表达式:preg_replace_callback 使用的语法错误?

发布于 2024-08-05 00:54:06 字数 1923 浏览 3 评论 0原文

我从此链接借用了代码 PHP 正则表达式模板 - 查找所有出现的 { {var}} 实现将值应用于模板文件的方法。这使用 preg_replace_callback() 函数,

我首选的命名方法是 name1.name2.name3=value,而不是 name1_name2_name3=value,但我使用的正则表达式似乎有问题。

这个不行。

模板文件

.aclass{
  font-width:{{newsflash.font.width}};
}

.ini 值

newsflash.font.width=8px

使用正则表达式

'!\{\{(\w+).+\.\w+\}\}!'

print_r($matches) 的输出

Array
(
  [0] => {{newsflash.font.width}}
  [1] => newsflash
)

替换是错误的,因为 $ matches[1] 是错误的密钥。

.aclass{
  font-width:;
}

我怀疑有些库已经提供了此功能并且很想了解它们,但我仍然想知道正则表达式的错误。

带有错误正则表达式的完整代码如下。

$inputFileName = 'templateVars.css';
$outputFileName = 'templateVals.css';
$varsFileName = 'variables.ini';

$ini_array = parse_ini_file($varsFileName);
$matchesArray = array();

function replace_value($matches) {
  global $ini_array;
  global $matchesArray;
  print "<pre>";
  print_r($matches);
  print "</pre>";
  return $ini_array[$matches[1]];
}


$inputFileVar = file_get_contents($inputFileName);

print "<pre>";
print_r($ini_array);
print "</pre>";


print "<pre>";
print $inputFileVar;
print "</pre>";

$outFileVar = preg_replace_callback('!\{\{(\w+).+\.\w+\}\}!', 'replace_value', $inputFileVar);

print "<pre>";
print $outFileVar;
print "</pre>";

print "<pre>";
print $matchesArray;
print "</pre>";

需要匹配的模板

.aclass{
  font-width:{{newsflash.font.width}};
  color:{{newsflash.font.color}}
}

.ini 文件的内容

newsflash.font.width=8px
newsflash.font.color=red

I have borrowed code from this link PHP regex templating - find all occurrences of {{var}} to implement a means of applying values to template fiies. This uses the preg_replace_callback() function

my preferred method of naming is name1.name2.name3=value, rather than name1_name2_name3=value, but the regex I am using seems to have a problem.

This one doesn't work.

template file

.aclass{
  font-width:{{newsflash.font.width}};
}

.ini values

newsflash.font.width=8px

regex used

'!\{\{(\w+).+\.\w+\}\}!'

output of print_r($matches)

Array
(
  [0] => {{newsflash.font.width}}
  [1] => newsflash
)

substitution is wrong because $matches[1] is wrong key key.

.aclass{
  font-width:;
}

I suspect there are libraries that already provide this functionality and would love to know about them, but I still want to know the fault with the regex.

The fullcode with the faulty regex is below.

$inputFileName = 'templateVars.css';
$outputFileName = 'templateVals.css';
$varsFileName = 'variables.ini';

$ini_array = parse_ini_file($varsFileName);
$matchesArray = array();

function replace_value($matches) {
  global $ini_array;
  global $matchesArray;
  print "<pre>";
  print_r($matches);
  print "</pre>";
  return $ini_array[$matches[1]];
}


$inputFileVar = file_get_contents($inputFileName);

print "<pre>";
print_r($ini_array);
print "</pre>";


print "<pre>";
print $inputFileVar;
print "</pre>";

$outFileVar = preg_replace_callback('!\{\{(\w+).+\.\w+\}\}!', 'replace_value', $inputFileVar);

print "<pre>";
print $outFileVar;
print "</pre>";

print "<pre>";
print $matchesArray;
print "</pre>";

Template to be matched

.aclass{
  font-width:{{newsflash.font.width}};
  color:{{newsflash.font.color}}
}

Contents of .ini file

newsflash.font.width=8px
newsflash.font.color=red

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

注定孤独终老 2024-08-12 00:54:06

字符 . 不是 \w 的一部分;并且您的 .+ (在分组括号之外)将匹配任何非空字符串(. 是通配符)。因此,$matches[1]=newsflash 是正确的。

您希望 $matches[1] 是什么?从你的问题中并不清楚这一点,抱歉。

the character . is not part of \w; and your .+ (outside the grouping parens) will match any nonempty string (. is wildcard). Therefore, $matches[1]=newsflash is correct.

What do you wnat the $matches[1] to be? This is not clear from your question, sorry.

疯了 2024-08-12 00:54:06
!\{\{(\w[\.\w]*)\}\};?!

这个正则表达式似乎与您的模板匹配得很好。

!\{\{(\w[\.\w]*)\}\};?!

this regexp seems to matching your templates fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文