Highlighting phrases

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
abushahin
Forum Commoner
Posts: 42
Joined: Wed Nov 25, 2009 12:35 pm
Location: london

Highlighting phrases

Post 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.
abushahin
Forum Commoner
Posts: 42
Joined: Wed Nov 25, 2009 12:35 pm
Location: london

Re: Highlighting phrases

Post 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.
solid
Forum Commoner
Posts: 28
Joined: Wed Aug 12, 2009 11:56 am

Re: Highlighting phrases

Post 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);
abushahin
Forum Commoner
Posts: 42
Joined: Wed Nov 25, 2009 12:35 pm
Location: london

Re: Highlighting phrases

Post by abushahin »

Fab, what I did was add a few more instead of just three words, that works perfect thank you.
abushahin
Forum Commoner
Posts: 42
Joined: Wed Nov 25, 2009 12:35 pm
Location: london

Re: Highlighting phrases

Post 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?
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Highlighting phrases

Post by omniuni »

Could you just do an

Code: Select all

str_replace('"','',$string);
?
abushahin
Forum Commoner
Posts: 42
Joined: Wed Nov 25, 2009 12:35 pm
Location: london

Re: Highlighting phrases

Post by abushahin »

omniuni wrote:Could you just do an

Code: Select all

str_replace('"','',$string);
?
Ive tried that it seems tho the quotes are attached to a character i.e "A
so stil no resolve.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Highlighting phrases

Post by requinix »

abushahin wrote:
omniuni wrote:Could you just do an

Code: Select all

str_replace('"','',$string);
?
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?
abushahin
Forum Commoner
Posts: 42
Joined: Wed Nov 25, 2009 12:35 pm
Location: london

Re: Highlighting phrases

Post by abushahin »

Hey, ye I am trying the str_replace on $cid and not on $verse.
lunarnet76
Forum Commoner
Posts: 67
Joined: Sun Apr 04, 2010 2:07 pm
Location: Edinburgh

Re: Highlighting phrases

Post 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;
abushahin
Forum Commoner
Posts: 42
Joined: Wed Nov 25, 2009 12:35 pm
Location: london

Re: Highlighting phrases

Post by abushahin »

Thanks for ur reply this has already been addressed now I'm having a problem getting rid of quote marks, any suggestions?
Post Reply