I got a file filter.txt with words that I want to highlight on a string.
Filter.txt: test1 test2
My solution doesen't work:
<?php
$file = file_get_contents('/home/user/filter.txt', true);
$keyword=$file;
$str="This is a test1 and test2 and test3";
$keyword = implode('|',explode(' ',preg_quote($keyword)));
$str = preg_replace("/($keyword)/i","<b>$0</b>",$str);
echo $str;
?>
Anyone?
took me like 15 mins but I eventually got it :)
<?php
$file = file_get_contents('/home/user/filter.txt', true);
$keywords = $file;
$str = "This is a test1 and test2 and test3";
$keywords = explode(" ", $keywords);
$str = preg_replace('/'.implode('|', $keywords).'/', '<b>$0</b>', $str);
echo $str;
?>
and this is expecting your filter.txt to be laid out like test1 test2 test3
etc. I'd suggest separating by new line or a pipe, |