php regex_replace在第一次匹配后停止


php regex_replace stop after first match

我正试图用-因此:
64px 64px #123456将变为:
-64px 64px #123456

我正在使用下一个正则表达式:
preg_replace("/((-*'d+(?:px|e[mx]|%)?)'s(-*'d+(?:px|e[mx]|%)?)){1,1}?/si", "-$1", $input_lines);

当只有2套尺寸时效果很好,但当有4套尺寸时,效果很好:
64px 64px 12px 12px #123456在中获取下一个结果:
-64px 64px -12px 12px #123456。第一次发生后,我该怎么做才能阻止它?谢谢

使用4。preg_replace的论点你可以限制你想做多少替换:

http://php.net/preg_replace

限制

The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

所以你应该使用这种方式:

preg_replace("/((-*'d+(?:px|e[mx]|%)?)'s(-*'d+(?:px|e[mx]|%)?)){1,1}?/si",
"-$1", $input_lines, 1);