I would like to use PHP's preg_replace()
to search a text for occurrences of a certain word, and enclose that word in brackets, unless there are already brackets present. The challenge here is that I want to test for brackets that may or may not be directly adjacent to the text I am looking for.
Random example: I want to replace warfarin
with [[warfarin]]
- in this string:
Use warfarin for the prevention of strokes
- but not in this string:
Use [[warfarin]] for the prevention of strokes
(brackets already present) - and not in this string either:
Use [[generic warfarin formulation]] for the prevention of strokes
('remote' brackets already present)
I can satisfy the first two requirements all right using lookbehind and lookahead assertions:
php > echo preg_replace( "/(?<!\[\[)(warfarin)(?!]])/", "[[$1]]", "Use warfarin for the prevention of strokes" );
Use [[warfarin]] for the prevention of strokes
php > echo preg_replace( "/(?<!\[\[)(warfarin)(?!]])/", "[[$1]]", "Use [[warfarin]] for the prevention of strokes" );
Use [[warfarin]] for the prevention of strokes
But I need your help with the third requirement, i.e. not adding brackets when there are 'remote' brackets present:
php > echo preg_replace( "/(?<!\[\[)(warfarin)(?!]])/", "[[$1]]", "Use [[generic warfarin formulation]] for the prevention of strokes" );
Use [[generic [[warfarin]] formulation]] for the prevention of strokes
In this last example, the square brackets should not be added to the word warfarin
since it is contained in a longer expression that is already enclosed in brackets.
The problem is that PHP's regexp assertions must have fixed length, otherwise it would be very simple.
I'm using
PHP 5.3.10-1ubuntu3.1 with Suhosin-Patch (cli) (built: May 4 2012 02:20:36)
Thanks in advance!
Try this:
I assume that there won't be any case of closing brackets without opening brackets.
This is what I would do.
will result in