Highlighting part of a word
Moderator: General Moderators
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
Highlighting part of a word
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"
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"
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
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"
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"
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
I have often wondered that myself.
Code: Select all
$output = str_ireplace($search, '<span class="highlight">'.$search.'</span>', $output);- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
If it is a straight replace why not use str_replace() instead? It is a lot less resource intensive.
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
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>
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>
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
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.
You should be able to do what you're looking for with only a basic understanding of regex patterns.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
function createSearchExpression($term) {
$term = explode('*', $term);
$term = array_map('preg_quote', $term);
return '/('. implode('.+', $term) .')/im';
}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 .*
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: