Regex
preg_match_all('@(\b' . preg_quote($needle,'@') . '\b)@is', $haystack, $matches);
Haystack
(Job: NAS-Inkrementell) Operation succeeded.
Needle
Operation succeeded
--> works
Operation succeeded.
--> does not work
I did some tests:
Alternative Haystack
(Job: NAS-Inkrementell) Op.eration succeeded.
Alternative Needle
Op.eration succeeded
--> works
So, the needle does get escaped correctly, as I can see by dump(preg_quote($needle,'@'));
--> Operation succeeded\.
What would be the correct way to include a trailing dot?
For quick reference: http://www.phpliveregex.com/p/hzl
Thanks
EDIT
I also need to distinguish between My-NAS
and My-NAS-2
in the haystack when the needle is My-NAS
, so I need the trailing \b
as well.
EDIT2
ideone.com/Suzz2E I want it to be 1 1 0 1 instead of 1 1 1 1, so MyNAS
should not be found if there is only MyNAS-2
in the haystack.