测试 preg_replace 不工作

tested preg_replace not working

我有以下

$string = 'Trials <- load.dataset("Foo, Bar")';  
$pattern[0] = "/(?:load.dataset|\"Foo, Bar?)\"/";

$replacement[0] = '"/my/path/JohnDoe.csv"';
preg_replace($pattern, $replacement, $string);

echo $pattern[0]; // /(?:load.dataset|"Foo, Bar?)"/
echo $replacement[0]; // "/my/path/JohnDoe.csv"

但是,替代品看起来不起作用;

echo $string; // Trials <- load.dataset("Foo, Bar")

我已经用 PHP Live Regex 测试了我的正则表达式,它在那里工作正常,所以我不确定我在哪里犯了错误。

为什么没有发生替换?

preg_replace 需要分配给您的变量。

是这样的:

$string = 'Trials <- load.dataset("Foo, Bar")';  
$pattern[0] = "/(?:load.dataset|\"Foo, Bar?)\"/";
$replacement[0] = '"/my/path/JohnDoe.csv"';
preg_replace($pattern, $replacement, $string);

即时替换,尝试更新为:

$string = preg_replace($pattern, $replacement, $string);

来自手册:

If matches are found, the new subject will be returned, otherwise subject will be returned unchanged or NULL if an error occurred.