php - Match exact word with any character regex -
how match exact word contains special character ?
$string = 'fall in love #pepsimoji! celebrate #worldemojiday downloading our keyboard @ http://bit.ly/pepsikb & take text game notch. - teacher'; preg_match("/\b#worldemojiday\b/i",$string); //false
i want match exact word containing character. if want match word 'download' in string, should return false
preg_match("/\bdownload\b/i",$string); //false
but when search downloading, should return true.
thanks
the problem \b
word boundary before #
non-word character. \b
cannot match position between 2 non-word (or between 2 word) characters, thus, not match.
a solution either remove first \b
, or use \b
(a non-word boundary matching between 2 word or 2 non-word characters) instead of it.
\b#worldemojiday\b
or
#worldemojiday\b
note \b
matches @ beginning of string.
here a way build regex dynamically, adding word boundaries necessary:
$srch = "žvolen"; $srch = preg_quote($srch); if (preg_match('/\w$/u', $srch)) { $srch .= '\\b'; } if (preg_match('/^\w/u', $srch)) { $srch = '\\b' . $srch; } echo preg_match("/" . $srch . "/ui", "žvolen used.");
Comments
Post a Comment