Solved

my sql text search

Posted on 2010-11-30
2
390 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 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