i need to declate a pattern for each string i want to match. if there way i can match right or left and replace according to that ?
if you found left replace with right, if you found right replace with left acrording to the order in the pattern
First replacing ‘left:’ with ‘new-right:’, then replace ‘right:’ with ‘left:’, and finally replace ‘new-right:’ with ‘right:’
To my knowledge, I can’t think of a way to accomplish what you want to do without a more work. The above approach will allow you to use three preg_replace calls and be done.
thanks you cpradio
but as passing the string and the regex as array can’t do it ?
if i try the regex as standalone it’s working but as array it doesn’t do nothing
sure it does. preg_replace takes 3 mixed variables; any of the three may be arrays. As per the manual, the behavior is:
“If (the replacement parameter) is a string and the pattern parameter is an array, all patterns will be replaced by that string. If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the replacement array than in the pattern array, any extra patterns will be replaced by an empty string.”
Maybe I’m wrong, but I took your prior post to mean that since $subject isn’t an array, the second iteration would use an empty string and not the updated subject from the first pass, which means, this method won’t work. You really would have to have 3 separate preg_replace calls. I haven’t had time to test this theory, but that is how I read the information you posted.