Solved

PHP regex    how to  replace with the same string

Posted on 2014-11-19
3
192 Views
Last Modified: 2014-11-19
the following statement works:

$display_html =
preg_replace(
   "/".$search_string."/i",
   "<b class='highlight'>".$search_string."</b>",
    $row['title'] );

where search_string is what is being searched for, and 'title'
is the  LIKE-matching result from a mysql database.

currently the regex substitutes the CASE that was used as typed in by the searcher.
I would like to have the CASE retained to that of what the field was in the database
(could be lower, initial cap, all caps, etc.)

how can the regex substitution (which surrounds the title search phrase with HTML tags for highlighting)
be altered to retain the the capitalization in the returned field result.
0
Comment
Question by:willsherwood
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40453578
Add in capturing parens around the thing being sought, then use the appropriate backreference (most likely 1) to refer to what was captured by the parens.

e.g.

$display_html = (
   "/(".$search_string.")/i", 
   "<b class='highlight'>$1</b>",
    $row['title'] );

Open in new window


Because I have only used one set of capturing parens, the backreference I use is for group 1 (that's the $1 bit in the replacement).
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40453587
Also, since you are searching for stuff based on what the user types in, you'll want to use the escape function (preg_quote) so that the user doesn't inadvertently (or maliciously) type in special regex characters into your input.

e.g.

$display_html = (
   "/(".preg_quote($search_string).")/i", 
   "<b class='highlight'>$1</b>",
    $row['title'] );

Open in new window

0
 

Author Closing Comment

by:willsherwood
ID: 40453592
thanks for the detailed help!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.

770 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