How could I convert something like this:
"hi (text here) and (other text)" come (again)
To this:
"hi \(text here\) and \(other text\)" come (again)
Basically, I want to escape “only” the parentheses that are inside quotes.
EDIT
I’m to new in Regex and so, I tried this:
$params = preg_replace('/(\'[^\(]*)[\(]+/', '$1\\\($2', $string);
But this will just escape the first occurrence of (.
EDIT 2
Maybe I should mention that my string could have these parentheses already escaped and, in this case, I don’t want to escaped them again.
By the way, I need it working for both double and single quotes but I think that I can do that as long as I have a working example for one of them.
This should do it for both single and double quotes:
output
It is for PHP >= 5.3. If you have a lower version (>=5) you have to replace the anonymous function in the callback with a separate function.