Page 1 of 1
Highlighting part of a word
Posted: Thu Nov 15, 2007 7:30 pm
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"
Posted: Thu Nov 15, 2007 7:53 pm
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"
Posted: Thu Nov 15, 2007 8:12 pm
by Jonah Bron
I have often wondered that myself.
Code: Select all
$output = str_ireplace($search, '<span class="highlight">'.$search.'</span>', $output);
Posted: Fri Nov 16, 2007 12:16 am
by Kieran Huggins
use a . in your matching expression for a single character wildcard, .* for zero to many characters, and .+ for one of more.
Posted: Fri Nov 16, 2007 2:02 pm
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.
Posted: Fri Nov 16, 2007 10:12 pm
by RobertGonzalez
If it is a straight replace why not use
str_replace() instead? It is a lot less resource intensive.
Posted: Fri Nov 16, 2007 11:39 pm
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>
Posted: Sat Nov 17, 2007 7:48 am
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.
Posted: Sat Nov 17, 2007 9:50 am
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 .*
Posted: Sat Nov 17, 2007 12:56 pm
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"
Posted: Sat Nov 17, 2007 6:22 pm
by s.dot
I believe you'll want to use .+?
Posted: Sat Nov 17, 2007 7:06 pm
by John Cartwright
scottayy wrote:I believe you'll want to use .+?
The ? will make it non-greedy, yes.