Using regex, I want to detect if a specific word exists within brackets in a string, if it does, remove the bracket and it's content.
The words I want to target are:
picture
see
lorem
So, here are 3 string examples:
$text1 = 'Hello world (see below).';
$text2 = 'Lorem ipsum (there is a picture here) world!';
$text3 = 'Attack on titan (is lorem) great but (should not be removed).';
What regex can I use with preg_replace()
:
$text = preg_replace($regex, '' , $text);
To remove these brackets and their content if they contain those words?
Result should be:
$text1 = 'Hello world.';
$text2 = 'Lorem ipsum world!';
$text3 = 'Attack on titan great but (should not be removed).';
Here's an ideone for testing.
You could use the following approach (thanks to @Casimir for pointing out an error before!):
See a demo on ideone.com and on regex101.com.
You can use this regex for searching:
Which means
and replace by empty string:
RegEx Demo
Code: