i.e.
$text = 'remove this text (keep this text and 123)';
echo preg_replace('', '', $text);
It should output:
(keep this text and 123)
This will do it: (and works with nested () as well)
$re = '/[^()]*+(\((?:[^()]++|(?1))*\))[^()]*+/';
$text = preg_replace($re, '$1', $text);
Here are a couple test cases:
Input:
Non-nested case: 'remove1 (keep1) remove2 (keep2) remove3'
Nested case: 'remove1 ((keep1) keep2 (keep3)) remove2'
Output:
Non-nested case: '(keep1)(keep2)'
Nested case: '(keep1) keep2 (keep3)'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With