Jump to content

Replace text with case sensitive input


eevan79

Recommended Posts

I have the auto suggestion script. While the user is typing, results are printed below (from the database). The results are printed with bold letters that are in the search. However, this leads to errors in capitalization. The letters are replaced by users' queries, instead of from the database.

 

Example:

Search word:

PHP

 

Results:

PHPfreaks,

PHP tutorials

 

But in the database this is the records: "phpfreaks" and "php tutorials".

 

 

This is a script that replaces search letters with bolded:

  $search = clean($_POST["search"]);

  $fname=$row['name']; //Record from database



  $re_fname='<b>'.$search.'</b>'; //Replace with bold letter


  $final_fname = str_ireplace($search, $re_fname, $fname);

 

 

How to do a script that bold letters, but as they were in the database, not from search input?

Link to comment
Share on other sites

You'll need to remember the match, which unfortunately involves preg_match():

 

$search = clean($_POST["search"]);
$junk = preg_match('/'.$search.'/i',$fname,$matches);
$whatyouwant = $matches[0];
$re_fname='<b>'.$whatyouwant.'</b>'; //Replace with bold letter
$final_fname = str_ireplace($search, $re_fname, $fname);

 

..that may be too much overhead.  I'd have to think about it harder, but that should work.

 

What auto suggestion script are you using?  I need a good one.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.