我正在尝试使用php中的preg_replace函数精确匹配一个字符串。 我只想匹配具有单个“@”符号的实例。 我还需要传入一个变量作为模式。
$x = "@hello, @@hello, @hello, @@hello"
$temp = '@hello'
$x = preg_replace("/".$temp."/", "replaced", $x);
结果应该只是这样的:
$x = "replaced, @@hello, replaced, @@hello"
Thank you in advance.
$temp
是动态的,请不要忘记使用preg_quote()
。 - HamZa