I have this:
$only_left_pattern = '/[^-{]\bleft\b/';
$subject = 'body {left: 24px;}\n p {right:30px;}';
$subject = preg_replace($only_left_pattern, 'right', $subject);
echo $subject;
I need to declare a pattern for each string I want to match. If there way I can match right or left and replace according to that? Example (syntax from my head):
$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);
What I'm asking if I can do something like my example with PHP or just with regular expression.
I tried Dave's solution
$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;
but it doesn't work, I tested the regex not as array and it's working.
preg_replace_callback(), although you can also pass arrays to the first and second arguments ofpreg_replace()- but I suspect a callback is what you want.