How could I convert something like this:
\"hi (text here) and (other text)\" come (again)
To this:
\"hi \\(text here\\) and \\(
You can use preg_replace_callback for this;
// outputs: hi \(text here\) and \(other text\) come (again)
print preg_replace_callback('~"(.*?)"~', function($m) {
return '"'. preg_replace('~([\(\)])~', '\\\$1', $m[1]) .'"';
}, '"hi (text here) and (other text)" come (again)');
What about already escaped strings;
// outputs: hi \(text here\) and \(other text\) come (again)
print preg_replace_callback('~"(.*?)"~', function($m) {
return '"'. preg_replace('~(?:\\\?)([\(\)])~', '\\\$1', $m[1]) .'"';
}, '"hi \(text here\) and (other text)" come (again)');