i.e.
$text = 'remove this text (keep this text and 123)';
echo preg_replace('', '', $text);
It should output:
(keep this text and 123)
i.e.
$text = 'remove this text (keep this text and 123)';
echo preg_replace('', '', $text);
It should output:
(keep this text and 123)
Here the 'non preg_replace' way:
Note:
- This extracts only the first pair of brackets.
- Replace strpos() with of strrpos() to get the last pair of brackets.
- Nested brackets cause trouble.
This will do it: (and works with nested
()
as well)Here are a couple test cases:
Take anything found within the brackets, put it in a capture group and keep that only, like this: