php - Highlight substring which can contain own extrac characters -
i need create function, highlights searched substring. example, have string "123456789" , search substring "234". it's easy, need create function:
str_replace('234', '123456789', '<b>234</b>');
but string can contains 4 (only four) special characters (" ", "(", ")", "-") need jump over.
so can have string "12 3(456)78-9)" , still need highlight searched "234", final string needs like
"1<b>2 3(4</b>56)78-9)"
do have suggestions how this?
edit: string , substring can contains character, not numbers example string: "poly(amide 610)" , searched word can "polyamide6" - highlighted "poly(amide 6"
you need put subpattern describe characters want skip between each character of target string:
$str = '12 3(456)78-9)'; $needle = '234'; $neutral = '[ ()-]*'; $pattern = '/' . implode($neutral, array_map(preg_quote, str_split($needle))) . '/iu'; $result = preg_replace($pattern, '<b>$0</b>', $str);
Comments
Post a Comment