Allmost.
The problem was that sometimes there was MATN in the string too, and that should not be replaced (reading the entire thread usually helps 🙂 )
Ok, hold on to your pants, here we go...
If you just want to replace any occurence of ATN, then just give a pattern that contains
exactly what you want to replace:
(the pattern must be delimited by two identical characters, it is customary to use slashes, but if you want the pattern itself to match
on slashes, you can use any other character)
$fldIGPMESSAGEDATA = "bla ATN MATN bla blah blah";
echo preg_replace("/ATN/", "ACCOUNT TELEPHONE NUMBER", $fldIGPMESSAGEDATA);
If you want to make the match case-insensitive, you can add an i after the pattern:
echo preg_replace("/ATN/i", "ACCOUNT TELEPHONE NUMBER", $fldIGPMESSAGEDATA);
Now, you want to match only ATN, not MATN, so you have to know that there is not an M in front of ATN.
You can make the pattern match on a set of characters by putting those characters in brackets: [agt] will match 'a' or 'g' or 't'.
You can make it match on 'anything BUT' those characters by adding a ^ sign: [agt] will match on anything BUT 'a', 'g' or 't'.
So to match on ATN preceded by NOT an M:
echo preg_replace("/[M]ATN/", "ACCOUNT TELEPHONE NUMBER", $fldIGPMESSAGEDATA);
But now you'll find that it replaces ATN, plus the character in front of it (which was not an M) That is because the 'NOT an M' characeter is part of the pattern that get's replaced.
You need to put that characters back in by using back-referencing.
In regexps you can define a block in the pattern by putting parentheses around it.
You can backreference to those blocks in the pattern itself and in the replacing string, by putting '\1' in to reference the first block, '\2' for the second etc.
In this case, I put parentheses around the 'NOT M' part, and I reference that block again at the start of the replacement string.
Now when the expression matches, it will replace '\1' with whatever it found in front of ANT that was not an M.
echo preg_replace("/([M])ATN/", "\1ACCOUNT TELEPHONE NUMBER", $fldIGPMESSAGEDATA);