I have pattern 'šalotka 29%'
and i need to know if string 'something something šalotka 29% something' contains the pattern
but not if the pattern is part of a longer word 'something something šalotka 29%something'
I have this mb_eregi('\b' . $pattern . '\b', $string)
but its not working because regex boundaries not working with special character.
Any suggestion?
A word boundary matches only between a word character (a character from the
\wcharacter class) and a non-word character or the limit of the string.If your searched string starts or ends with a non-word character, you can't use a word-boundary.
The difficulty is to define yourself precisely what separates the desired chain from the rest. In other words, it is your choice. Whatever your choice is, you can use the same technique: using lookarounds before and after your string to define what you don't want around your string: a negative lookbehind
(?<!...)and a negative lookahead(?!...).Example:
full example:
Notices:
If
eregfunctions are now obsolete and have been removed from recent PHP versions,mb_eregfunctions, based on the oniguruma regex engine, still exist and offer features not available inpreg_functions (PCRE).Obviously for this current question, you can do the same with
preg_match:With
preg_functions you can usepreg_quoteto escape them, but it's also possible to "do it yourself" with$item = mb_ereg_replace('[\[\](){}.\\\\|$^?+*#-]', '\\\0', $item);that suffices for most of the syntaxes available inmb_eregfunctions (Note that escaping all non-word characters does the job too). Feel free to write your own if you want to deal with Emacs or BRE syntaxes.