使用preg_replace查找和替换两侧用花括号括起来的逗号的正则表达式是什么?两边用花括号括起来?
示例:
$subject = "blah.blah,{blah.blah},blah.blah";
$result = "{blah.blah},{blah.blah},{blah.blah}";
避免这种和其他不必要的变化:
$result = "{blah.blah},{{blah.blah}},{blah.blah}";
答案 0 :(得分:1)
preg_replace("/(?<=^|,)([^{},]+)(?=$|,)/", '{$1}', "blah.blah,{blah.blah},blah.blah");
答案 1 :(得分:0)
答案有效,但有一个更好的正则表达式导致后视和后视对于机器来说是昂贵的:
preg_replace('/(^|,)([^{},]+)($|,)/', '$1{$2}$3', 'blah.blah,{blah.blah},blah.blah');