Look at this simple script please
$a = "test";
echo $b = preg_replace('/[^a](e)/',"<b>$1</b>",$a); // returns <b>e</b>st
I want to bold the “e” characters, when there is no “a” character before it.
Logically in $0 it must match "te", and in $1 – "e", but why it strips the first character in my example?
I can solve the task in another way, but i want to understand this behavior.
Thanks much
Because the match is two characters wide: the
e, and the character before it (which is denoted by[^a]).To change this, there are two ways. The easy is just to parenthesize your match:
The second is to use a negative lookbehind: