Solved

PHP regex    how to  replace with the same string

Posted on 2014-11-19
3
189 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
thanks for the detailed help!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now