Page 1 of 1
Highlighting phrases
Posted: Sun Apr 11, 2010 1:37 pm
by abushahin
Hey im currently using
str_replace to highlight words in a search results page. First its not case insensitive because its not
str_ireplace, so thats not a problem. But the problem is that when I replace the strings it only highlights the exact phrase. I.e.
Code: Select all
$cid = "Please dont ask";
$verse = "I have to go Please I am running late, dont ask, Please dont ask";
$d = str_replace( $cid, "<span style='color:red'>".$cid."</span>", $verse);
echo $d;
Returns: I have to go
Please I am running late,
dont ask,
Please dont ask
The words highlighted in yellow dont get highlighted, I know why but can anyone suggest of a way to explode the words and then use
str_replace so that I can highlight all the words even if they are not mentioned together.
Re: Highlighting phrases
Posted: Sun Apr 11, 2010 6:13 pm
by abushahin
Ok I managed to find the answer, I guess there is many ways of doing it but this way certainly works.
Code: Select all
$allinput = explode(" ",$cid);
$allinput[0];
$allinput[1];
$allinput[2];
$input = explode(" ",$cid);
$input[0] = "<span style='color:red'>".$allinput[0]."</span>";
$input[1] = "<span style='color:red'>".$allinput[1]."</span>";
$input[2] = "<span style='color:red'>".$allinput[2]."</span>";
$placeholders = array($allinput[0], $allinput[1], $allinput[2]);
$plrep = array($input[0], $input[1], $input[2]);
?>
<?= $repverse = str_replace($placeholders, $plrep, $verse;);?><br />
Only thing I'd like to know is what issues am a likely to face i.e. server loads, security etc etc any suggestions appreciated.
Re: Highlighting phrases
Posted: Sun Apr 11, 2010 10:47 pm
by solid
Unless you will always have 3 words, use something more like this...
Code: Select all
// assumes $cid is your query string, and $verse is your body text
$highlighted = array();
$words = explode(' ', $cid);
foreach ($words as $word)
{
$highlighted[] = '<span style="color:red">'.$word.'</span>';
}
echo str_replace($words, $highlighted, $verse);
Re: Highlighting phrases
Posted: Tue Apr 13, 2010 9:05 am
by abushahin
Fab, what I did was add a few more instead of just three words, that works perfect thank you.
Re: Highlighting phrases
Posted: Tue Apr 13, 2010 7:04 pm
by abushahin
Ok Im having this problem where if a word is inside a qoutes (" ") that will not be seen as a separate word i.e "Good things happen" only the word things gets highlighted (str_replace) where as the words "Good and things" dont get replaced, i know its because those words have the quotes attached to it but is there a way to resolve this, I know I can use regex to do this but is there an easier way?
Re: Highlighting phrases
Posted: Tue Apr 13, 2010 10:26 pm
by omniuni
Re: Highlighting phrases
Posted: Sun Apr 18, 2010 1:13 pm
by abushahin
omniuni wrote:Could you just do an
?
Ive tried that it seems tho the quotes are attached to a character i.e "A
so stil no resolve.
Re: Highlighting phrases
Posted: Sun Apr 18, 2010 5:18 pm
by requinix
abushahin wrote:omniuni wrote:Could you just do an
?
Ive tried that it seems tho the quotes are attached to a character i.e "A
so stil no resolve.
You are running that bit of code on $cid and not $verse, right?
Re: Highlighting phrases
Posted: Sun Apr 18, 2010 6:15 pm
by abushahin
Hey, ye I am trying the str_replace on $cid and not on $verse.
Re: Highlighting phrases
Posted: Sun Apr 18, 2010 6:27 pm
by lunarnet76
in case you did not know you can simply use
Code: Select all
$cid =array("Please","dont","ask");
$cidReplace =array("<span style='color:red'>Please</span>","<span style='color:red'>dont</span>","<span style='color:red'>ask</span>");
$verse = "I have to go Please I am running late, dont ask, Please dont ask";
$d = str_replace( $cid, $cidReplac, $verse);
echo $d;
Re: Highlighting phrases
Posted: Sun Apr 18, 2010 6:33 pm
by abushahin
Thanks for ur reply this has already been addressed now I'm having a problem getting rid of quote marks, any suggestions?