- preg_match_all: documentation ( source)
- implode: documentation ( source)
- preg_replace: documentation ( source)
<?php
function highlight($text, $words) {
preg_match_all ( '~[\w\'\"]*~', $words, $m );
if (! $m)
return $text;
$re = '~\\b(' . implode ( '|', $m [0] ) . ')\\b~i';
return preg_replace ( $re, '<em>$0</em>', $text );
}
$text = "this is Mechanic's test";
$word = "Mechanic's";
echo highlight($text,$word);
$text = 'this is Mechanic"s test';
$word = 'Mechanic"s';
echo "\n".highlight($text,$word);
$text = 'this is Mechanics test';
$word = 'Mech';
echo "\n".highlight($text,$word);