Skip to main content
Inspiring
February 22, 2007
Question

OT: Highlighting keywords in content - PHP

  • February 22, 2007
  • 13 replies
  • 641 views
I have a document displayed on a page and I want to highlight keywords in
the document's text. My simple (and usually befuddled) mind tells me to
begin attempting this in the following way -

1. Load the document contents into an array (with explode).
2. Load the keywords list into an array.
3. For each word in the document (i.e., element in the contents array),
scan the keyword list for a match, and if there is a match, add a <span
class="highlight"></span> around it.
4. Display the document.

I want to do it this way to accommodate a dynamic keyword list.

The documents themselves are pretty short - maybe 5 - 6 paragraphs.

Am I thinking about this the right way?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================



This topic has been closed for replies.

13 replies

Inspiring
February 22, 2007
Yes, its one of those things that sounds complex, but is actually really
easy :-)

Just do a standard batch replace, it doesn't matter if the keywords not
found as nothing will be done, although the keyword should be in there if
its a search result. You just need a couple of replaces to make sure you hit
all combinations of the word eg KEYWORD,keyword,Keyword. The str_replace()
command is faster than using regular expressions, so its the prefered
method.

--
Gareth
http://www.phploginsuite.co.uk/
PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.


Inspiring
February 22, 2007
Gareth, this is really easier than I had thought. You are doing a batch
replacement instead of looking for a match first. Brilliant.

Thanks!

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"gareth" <support@nospam_phploginsuite.co.uk> wrote in message
news:erl1od$d9p$1@forums.macromedia.com...
> Yes, your spot on. I`d always thought it would be complicated, but decided
> to give it a go the other day, and realised that it was actually
> incredibly simple when I thought about it, one of those DOH! moments :-)
>
> In PHP its as simple as:
>
> $text = str_replace($keyword,"<span class='highlight'>" . $keyword .
> "</span>",$text);
>
> Its best to create a custom function to do the above looping through the
> list of keywords, and using ucase() etc to get words where the whole word
> is in uppercase, lower case, or the first letter is uppercase.
>
> Looks really effective!
>
> --
> Gareth
> http://www.phploginsuite.co.uk/
> PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.
>


Inspiring
February 22, 2007
Yes, your spot on. I`d always thought it would be complicated, but decided
to give it a go the other day, and realised that it was actually incredibly
simple when I thought about it, one of those DOH! moments :-)

In PHP its as simple as:

$text = str_replace($keyword,"<span class='highlight'>" . $keyword .
"</span>",$text);

Its best to create a custom function to do the above looping through the
list of keywords, and using ucase() etc to get words where the whole word is
in uppercase, lower case, or the first letter is uppercase.

Looks really effective!

--
Gareth
http://www.phploginsuite.co.uk/
PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.