In a PHP variable I have some text that contains some keywords. These keywords are currently capitalised. I would like them to remain capitalised and be wrapped in curly brackets but once only. I am trying to write upgrade code but each time it runs it wraps the keywords in another set of curly brackets.
What REGEX do I need to use to match the keyword alone without also matching it if it is {KEYWORD}.
For example, the text variable is:
$string = "BLOGNAME has posted COUNT new item(s),
TABLE
POSTTIME AUTHORNAME
You received this e-mail because you asked to be notified when new updates are posted.
Best regards,
MYNAME
EMAIL";
And my upgrade code is:
$keywords = array('BLOGNAME', 'BLOGLINK', 'TITLE', 'POST', 'POSTTIME', 'TABLE', 'TABLELINKS', 'PERMALINK', 'TINYLINK', 'DATE', 'TIME', 'MYNAME', 'EMAIL', 'AUTHORNAME', 'LINK', 'CATS', 'TAGS', 'COUNT', 'ACTION');
foreach ($keywords as $keyword) {
$regex = '|(^\{){0,1}(\b' . $keyword . '\b)(^\}){0,1}|';
$replace = '{' . $keyword . '}';
$string = preg_replace($regex, $replace, $string);
}
My REGEX is currently not working well at all, it is stripping some spaces and also on each run placing more curly brackets around most (but not all) keywords. What am I doing wrong? Can someone correct my regex?