Hello,
I have a script that allows a user to search one of my databases for information within a certain mysql field. Upon returning the results, I want to format the text so as to highlight the search term and only show about 10 words before and after the search term.
For example if the search term is "hello" and the searched field is:
"Hi I'm a dumb newbie posting on phpbuilder.net. You can tell this because I still start my posts with hello. That is a sure sign that I'm trying to get everyone to like me and respond to my post."
I would like it to return:
"...can tell this because I still start my posts with <span class="highlight">hello</span>. That is a sure sign that I'm trying to get..."
I've been able to replace the search term with the correct HTML using the following code:
$search_term = '/'.$term.'/';
$replacement = "<span class=\"highlight\">$term</span>";
$text = preg_replace($search_term, $replacement, $row['text']);
Can you help me then select the previous bit of text and the following bit of text? I understand that I probably cannot get whole words, but if that is possible I would be so happy...I would really learn a lot there!
Thanks so much!