How could I convert something like this:
\"hi (text here) and (other text)\" come (again)
To this:
\"hi \\(text here\\) and \\(
Here's how you can do it with the preg_replace_callback() function.
$str = '"hi (text here) and (other text)" come (again)';
$escaped = preg_replace_callback('~(["\']).*?\1~','normalizeParens',$str);
// my original suggestion was '~(?<=").*?(?=")~' and I had to change it
// due to your 2nd edit in your question. But there's still a chance that
// both single and double quotes might exist in your string.
function normalizeParens($m) {
return preg_replace('~(?