Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP regex    how to  replace with the same string

Posted on 2014-11-19
3
Medium Priority
?
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

721 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