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
https://forums.phpfreaks.com/topic/232828-replace-text-with-case-sensitive-input/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.