I’m trying to find a keyword (ex. “dog bone”) in a paragraph string (ex. “I bought my lucky dog a new dog bone and he loves it.” using PHP preg_match & preg_replace. I found some code (from early 2000s) that works ok but is slow.
Anyone know of a good alternative or better way to write this code?
$tmp = "dog bone"
$string = "I bought my lucky dog a new dog bone and he loves it."
preg_match("/(\\w+)? ?(\\w+)? ?(\\w+)? ?(\\w+)? ?(\\w+)? ?$tmp? ?(\\w+)? ?(\\w+)? ?(\\w+)? ?(\\w+)? ?(\\w+)?/i", $string, $result);
if ($result[0] != "") {
$my_output = preg_replace("/$tmp/i","<B>$tmp</B>",$result[0]);
}
echo $my_output;