PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am making a search, and I need a function to scan through the results, and replace keywords with highlighted keywords. The one I have is a pathetic attempt. I think I asked this question a long time ago, but never came back to see the response. HEre's my function.
I am completely lost... how would I do this? I don't know regex very well. Anybody know regex feel like making a function or at lease pointing me in the right direction?
I tried it right after you posted it... thanks, but I can't get it to work. I may be doing something wrong. If you look at it, there is a function called createfunction() that I don't really understand. He probably was implying for me to make a function to do whatever it needs to do, but I don't even understand what it's supposed to do. Confusing. Thanks for your help, if you know what he meant, I'd appreciate an explanation if you don't mind.
function highlightText($string_to_highlt,$a_hightlight_words){
if(!is_array($a_hightlight_words)){
$a_hightlight_words = explode(" ", $a_hightlight_words);
}
$needle = join('|',$a_hightlight_words);
// use the | to make preg_replace to use multiple values
// use the /i to make case insensitive and \\0 not replace th word but simply to wrap the word.
return preg_replace("/($needle)/i",'<span class="highlighttxt">\\0</span>',$string_to_highlt);
}
Oh... he put in createfunction instead of create_function, but it still doesn't work and I'm sure its something I'm doing. I'll dink with it some more.