yami007 Posted January 11, 2010 Share Posted January 11, 2010 this is my simple code <?php $search = "new day has come a"; //$search = "a"; $keywords = preg_split("/[\s,]+/", "$search"); $patterns = array(); $replacements = array(); foreach( $keywords as $key ) { $patterns[] = "/$key/"; } foreach( $keywords as $key ) { $replacements[] = "<b>$key</b>"; } echo preg_replace($patterns, $replacements, 'a new day has come'); ?> the result is : a new day has come why does a takes over the others ?? Link to comment https://forums.phpfreaks.com/topic/188048-my-basic-search-color-keywords/ Share on other sites More sharing options...
JAY6390 Posted January 11, 2010 Share Posted January 11, 2010 It goes through the array in order, and as it matched a before day it changed it to d<b>a</b>y. The simplest thing you can do is this <?php $search = "new day has come a"; //$search = "a"; $keywords = preg_split("/[\s,]+/", "$search"); $patterns = array(); $replacements = array(); foreach ($keywords as $key) { $patterns[] = '/\b'.preg_quote($key).'\b/i'; $replacements[] = "<b>$key</b>"; } echo preg_replace($patterns, $replacements, 'a new day has come'); ?> Link to comment https://forums.phpfreaks.com/topic/188048-my-basic-search-color-keywords/#findComment-992744 Share on other sites More sharing options...
yami007 Posted January 11, 2010 Author Share Posted January 11, 2010 yeah i know but it's not what i am doing what if the user searches for it as " a new day has come " how can i search for " a " and leave it to the last of the array ?? Link to comment https://forums.phpfreaks.com/topic/188048-my-basic-search-color-keywords/#findComment-992748 Share on other sites More sharing options...
JAY6390 Posted January 11, 2010 Share Posted January 11, 2010 If you use my code above you will see that it matches all of them. This is done by adding the \b to either side of the regex (which is the boundary shorthand) and this means it will not match mid word, only full words Link to comment https://forums.phpfreaks.com/topic/188048-my-basic-search-color-keywords/#findComment-992749 Share on other sites More sharing options...
yami007 Posted January 11, 2010 Author Share Posted January 11, 2010 If you use my code above you will see that it matches all of them. This is done by adding the \b to either side of the regex (which is the boundary shorthand) and this means it will not match mid word, only full words sorry i misunderstood :s* thank you : ) Link to comment https://forums.phpfreaks.com/topic/188048-my-basic-search-color-keywords/#findComment-992752 Share on other sites More sharing options...
cags Posted January 11, 2010 Share Posted January 11, 2010 I really don't understand the objective here, because I have to assume it's not to simply make each word bold. If that is the objective why not use the much simpler... preg_replace('#\b([a-z]+)\b#i', '<b>$1</b>', $input); Link to comment https://forums.phpfreaks.com/topic/188048-my-basic-search-color-keywords/#findComment-992754 Share on other sites More sharing options...
JAY6390 Posted January 11, 2010 Share Posted January 11, 2010 It's to highlight all the words in a text with the search terms. The text would obviously be longer than the test text Link to comment https://forums.phpfreaks.com/topic/188048-my-basic-search-color-keywords/#findComment-992755 Share on other sites More sharing options...
yami007 Posted January 11, 2010 Author Share Posted January 11, 2010 It's to highlight all the words in a text with the search terms. The text would obviously be longer than the test text yeah well, excuse me to ask again, assume the text is : " if today was your last day " i dont want to highlight a everywhere but i want to highlight day i wonder how i can do this !!! Link to comment https://forums.phpfreaks.com/topic/188048-my-basic-search-color-keywords/#findComment-992759 Share on other sites More sharing options...
cags Posted January 11, 2010 Share Posted January 11, 2010 It's to highlight all the words in a text with the search terms. The text would obviously be longer than the test text Ahh I see the replace is being ran on a separate input. It's to highlight all the words in a text with the search terms. The text would obviously be longer than the test text yeah well, excuse me to ask again, assume the text is : " if today was your last day " i dont want to highlight a everywhere but i want to highlight day i wonder how i can do this !!! Since JAY6390's pattern matches \b$word\b it will not replace an a unless it is just an a. \b matches word boundaries. Link to comment https://forums.phpfreaks.com/topic/188048-my-basic-search-color-keywords/#findComment-992762 Share on other sites More sharing options...
JAY6390 Posted January 11, 2010 Share Posted January 11, 2010 The only way to do that would be to remove the a from the array before hand. You could create a list of unwanted words and remove those before making the regex patterns to have the <b> Link to comment https://forums.phpfreaks.com/topic/188048-my-basic-search-color-keywords/#findComment-992763 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.