Solved

PHP regex    how to  replace with the same string

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

790 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