Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

my sql text search

Posted on 2010-11-30
2
Medium Priority
?
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The viewer will learn how to count occurrences of each item in an array.
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.

670 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