Solved

my sql text search

Posted on 2010-11-30
2
371 Views
Last Modified: 2012-05-10
i am using mysql and php and i am doing search in a field which is of type text. i would like a part of sentence to be displayed with the search string highlighted.  
example:

i search for ->    bob

text field has --> this is test. this is test. my name is bob. this is test. this is test.

result --> my name is bob, with bob highlighted
0
Comment
Question by:nandananushil1
2 Comments
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34238414
Make a select that return you a record if the text contains search string or null if it doesn't contain it.

Once you have the record you'll need to strip it, use split to convert the string into an array of phrases separated by ".". Now loop through that array seeking in every element if the search string is present, when you find an element of the array with the search string present exit the loop and return to your page the string contained on that array's element with <strong></strong> inserted before and after the search string.
0
 
LVL 5

Accepted Solution

by:
JermTheWorm earned 500 total points
ID: 34238500
Since you don't explain how you are 'doing search' or how you are doing your displaying, I'm going to assume that you are doing an echo of a retuned value from a php mysql query that uses a 'like %$srchvar%'.

Assuming:
  echo $result;
would print your result normally you can do:
   echo str_replace($srchvar,'<em>'.$srchvar.'</em>',$result);
replace the '<em>' and '</em'> with whatever html tags you want to use to highlight the searched for term.

HTH
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now