Highlighting part of a word

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Highlighting part of a word

Post by tecktalkcm0391 »

I am having diffculties trying to think of a way to do this...

I have a search term... say "ch"... and the string "Christopher"... how can I make it so php echos: "<span class="highlight">Ch</span>ristopher"
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

I got this to work by using

preg_replace("/($search)/i", '<span class="suggest_highlight">$1</span>', $s_suggest);

.... but now I have another question... I want to use a wildcard of * so... If the search is "ch*is" and the string is again "Christopher".... how can I get php to add the <span> around "Chris"
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

I have often wondered that myself.

Code: Select all

$output = str_ireplace($search, '<span class="highlight">'.$search.'</span>', $output);
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

use a . in your matching expression for a single character wildcard, .* for zero to many characters, and .+ for one of more.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

I'm confused... use .* if it should only do it once, and .+ for more than one time?... and where do i put it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If it is a straight replace why not use str_replace() instead? It is a lot less resource intensive.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Because its coming from a user so it will be different everytime....

I just cant figure out how to highlight parts of the string, and use a wildcard also.

Examples:
String = "Chris is not understanding".... Search = "C* understand".... would highlight from "C.." to "..stand"
String = "Chris is not understanding".... Search = "C* is".... would highlight from "C.." to "..is"
String = "Joseph Smith".... Search = "J Smith".... would highlight both "J" and "Smith"

Note: by would highlight... I mean put <div class="highlight"> TERM/LETTER/PHRASE TO HIGHLIGHT </div>
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

take a browse through http://www.regular-expressions.info

You should be able to do what you're looking for with only a basic understanding of regex patterns.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

function createSearchExpression($term) {
   $term = explode('*', $term);
   $term = array_map('preg_quote', $term);
   return '/('. implode('.+', $term) .')/im';
}
A couple things you have to remember..
When using user input you must always escape using preg_quote() to avoid the user inputting regex characters maliciously.
Secondly, you need to convert your wildcard into .+ to make it matches one or more characters (which I believe you said you wanted). If you want it to match zero or more characters then you'll need to change the code to .*
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Thanks! --- It worked.... but it has one problem...

If i use the string... "Chris is happy" and search ch*s it highlights "Chris is" instead of "Chris"
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I believe you'll want to use .+?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

scottayy wrote:I believe you'll want to use .+?
The ? will make it non-greedy, yes.
Post Reply