I have a string Action – [N]ew, [U]pdate, or [D]elete : N that I need to replace with “Action – [N]ew, [U]pdate, or [D]elete : U” somhow by using preg_replace I can’t get it working. It remains the same.
My code looks like this
$action = Action - '[N]ew, [U]pdate, or [D]elete : U';
$line = preg_replace("/(Action - [N]ew, [U]pdate, or [D]elete : N)/",$action,$line);
[and]are special characters in regular expressions. You’ll have to escape them if you want to match them:Without being escaped, and expression within
[and]will match one of every character within them. So in your original case,"[N]ew"was matching"New". If it had been"[NP]ew", it would have matched"New"or"Pew".