Highlight text, except html tags

前端 未结 3 541
抹茶落季
抹茶落季 2021-01-18 06:26

I\'m using the code below to highlight some keywords in a text:

$message = str_ireplace($words,\'\'.$words.\'         


        
3条回答
  •  Happy的楠姐
    2021-01-18 07:06

    Better code based on reply from @Savageman

    $str = 'ba';
    $highlightWhat = "ba";
    $str = preg_replace_callback('#((?:(?!<[/a-z]).)*)([^>]*>|$)#si', function($m) use ($highlightWhat) {
                return preg_replace('~('.$highlightWhat.')~i', '$1', $m[1]) . $m[2];
            },
            $str);
    

提交回复
热议问题