You are trying to make a specific word, i.e. worldbold? If yes, then one way could be:
function make_bold($str, $replace) {
$words = array_intersect(str_word_count(strtolower($str), 2), $replace);
if(count($words) > 0) {
$rand_pos = array_rand($words);
$str = substr_replace($str, '<b>' . $words[$rand_pos] . '</b>', $rand_pos, strlen($words[$rand_pos]));
}
return $str;
}
used as:
$str = make_bold($str, array('world'));
Link: str_word_count , array_intersect, array_rand,substr_replace
Take a look DEMO .
source
share