Solved

PHP regex    how to  replace with the same string

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link Stopped Working 7 33
How can I send attachment with mail 5 26
Undefined variable with $_POST in PHP 5 40
How do I init a PHP page jusing JQuery? 8 32
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…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

710 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