我有这个:
$only_left_pattern = '/[^-{]\bleft\b/';
$subject = 'body {left: 24px;}\n p {right:30px;}';
$subject = preg_replace($only_left_pattern, 'right', $subject);
echo $subject;
我需要为要匹配的每个字符串声明一个模式。如果有办法我可以左右匹配并据此替换?示例(我脑海中的语法):
$right_left_pattern = '/[^-{](\bleft\b|\bright\b/)';
// if you found left replace with right, if you found right replace with left
// acrording to the order in the pattern
$subject = preg_replace($only_left_pattern, right|left, $subject);
我在问我是否可以使用 PHP 或仅使用正则表达式来执行类似我的示例的操作。
我尝试了戴夫的解决方案
$pattern = array('/[^-{]\bleft\b/','/[^-{]\bright\b/');
$replace = array('right','left');
$subject = 'body {left: 24px;} p {right: 24px;}';
$subject = preg_replace($pattern, $replace, $subject);
echo $subject;
但它不起作用,我测试了regex
not as 数组并且它正在工作。