Solved

my sql text search

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.
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…
The viewer will learn how to count occurrences of each item in an array.

830 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