I have this code :
$str=preg_replace(
'#\b[^"](Hello User)#',
'<a href="$1">$1</a>',
$str);
return nl2br($str);
So, I’d like to replace every occurences of Hello User that doesnt start with ".
For example with string :
Hello User\n
"Hello User\n
Hello User\n
"Hello User"\n
I’ll aspect this result :
<a href="Hello User">Hello User</a><br />
"Hello User<br />
<a href="Hello User">Hello User</a><br />
"Hello User"<br />
But in fact the output is the opposite :
Hello User<br />
"<a href="Hello User">Hello User</a><br />
Hello User<br />
"<a href="Hello User">Hello User</a>"<br />
Why? And how can I fix this trouble?
EDIT
You can see an example here http://codepad.org/2Q466lx2
The reason is that
[^"]expects to match a character (anything but a"). Since your firstHello Useris at the start of the string, it fails.Use a negative lookbehind assertion instead:
This matches
Hello Useronly if it is not preceded by a".