Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

OT: Highlighting keywords in content - PHP

LEGEND ,
Feb 22, 2007 Feb 22, 2007

Copy link to clipboard

Copied

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
==================



TOPICS
Server side applications

Views

615
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2007 Feb 22, 2007

Copy link to clipboard

Copied

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.


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2007 Feb 22, 2007

Copy link to clipboard

Copied

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.
>


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2007 Feb 22, 2007

Copy link to clipboard

Copied

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.


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2007 Feb 23, 2007

Copy link to clipboard

Copied

Hmm - so why isn't this working?

<?php

$content=$row_rsContent['archContent'];
foreach ($row_Recordset1 as $jargon) {
str_replace($jargon,'****',$content);
}
echo $content;

?>

--
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:erl3b0$f4d$1@forums.macromedia.com...
> 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.
>


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2007 Feb 23, 2007

Copy link to clipboard

Copied

$content=$row_rsContent['archContent'];
foreach ($row_Recordset1 as $jargon) {
str_replace($jargon,'****',$content);
}

Your not putting the result of str_replace anywhere, it has to be assigned
to a variable so it can be used

eg $new_text = str_replace($jargon,'****',$content);

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


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2007 Feb 23, 2007

Copy link to clipboard

Copied

Duh. I suppose that makes sense.... 8)

--
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:ermo6s$d72$1@forums.macromedia.com...
> $content=$row_rsContent['archContent'];
> foreach ($row_Recordset1 as $jargon) {
> str_replace($jargon,'****',$content);
> }
>
> Your not putting the result of str_replace anywhere, it has to be assigned
> to a variable so it can be used
>
> eg $new_text = str_replace($jargon,'****',$content);
>
> --
> Gareth
> http://www.phploginsuite.co.uk/
> PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.
>


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2007 Feb 23, 2007

Copy link to clipboard

Copied

OK - now I have this -

<?php

$content=$row_rsContent['archContent'];
foreach ($row_Recordset1 as $jargon) {

echo $jargon."<br>";
$content=str_replace($jargon,'<span
style="background-color:yellow;">'.$jargon.'</span>',$content);
$content=str_replace(ucfirst($jargon),'<span
style="background-color:yellow;">'.ucfirst($jargon).'</span>',$content);


}

echo $content;

?>

But it doesn't seem to be do the 'foreach' properly. I only get the first
element of the associative array. Would I need to convert it to a numeric
array?

--
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
==================


"Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
news:ermpep$elq$1@forums.macromedia.com...
> Duh. I suppose that makes sense.... 8)
>
> --
> 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:ermo6s$d72$1@forums.macromedia.com...
>> $content=$row_rsContent['archContent'];
>> foreach ($row_Recordset1 as $jargon) {
>> str_replace($jargon,'****',$content);
>> }
>>
>> Your not putting the result of str_replace anywhere, it has to be
>> assigned to a variable so it can be used
>>
>> eg $new_text = str_replace($jargon,'****',$content);
>>
>> --
>> Gareth
>> http://www.phploginsuite.co.uk/
>> PHP Login Suite V2 - 34 Server Behaviors to build a complete Login
>> system.
>>
>
>


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2007 Feb 23, 2007

Copy link to clipboard

Copied

On 23 Feb 2007 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:

> OK - now I have this -
>
> <?php
>
> $content=$row_rsContent['archContent'];
> foreach ($row_Recordset1 as $jargon) {
>
> echo $jargon."<br>";
> $content=str_replace($jargon,'<span
> style="background-color:yellow;">'.$jargon.'</span>',$content);
> $content=str_replace(ucfirst($jargon),'<span
> style="background-color:yellow;">'.ucfirst($jargon).'</span>',
$content);
> }
>
> echo $content;
>
> ?>
>
> But it doesn't seem to be do the 'foreach' properly. I only get the
> first element of the associative array. Would I need to convert it
> to a numeric array?

You want to do this on $content, right?

$content=$row_rsContent['archContent'];
foreach ($content as $jargon) {

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/email.php

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2007 Feb 23, 2007

Copy link to clipboard

Copied

No - I want to do it on the associative array $row_rsRecordset1, which
contains the jargon words that I want to find in $content.

--
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
==================


"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns98E058D4ACEEAmakowiecatnycapdotrE@216.104.212.96...
> On 23 Feb 2007 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:
>
>> OK - now I have this -
>>
>> <?php
>>
>> $content=$row_rsContent['archContent'];
>> foreach ($row_Recordset1 as $jargon) {
>>
>> echo $jargon."<br>";
>> $content=str_replace($jargon,'<span
>> style="background-color:yellow;">'.$jargon.'</span>',$content);
>> $content=str_replace(ucfirst($jargon),'<span
>> style="background-color:yellow;">'.ucfirst($jargon).'</span>',
> $content);
>> }
>>
>> echo $content;
>>
>> ?>
>>
>> But it doesn't seem to be do the 'foreach' properly. I only get the
>> first element of the associative array. Would I need to convert it
>> to a numeric array?
>
> You want to do this on $content, right?
>
> $content=$row_rsContent['archContent'];
> foreach ($content as $jargon) {
>
> --
> Joe Makowiec
> http://makowiec.net/
> Email: http://makowiec.net/email.php


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2007 Feb 23, 2007

Copy link to clipboard

Copied

On 23 Feb 2007 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:

> No - I want to do it on the associative array $row_rsRecordset1, which
> contains the jargon words that I want to find in $content.

But $row_rsRecordset1 is the associative array containing everything from
one row in the result set. An individual field from within that array
would be $row_rsRecordset1['myTextField'].

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/email.php

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2007 Feb 23, 2007

Copy link to clipboard

Copied

Hmm -

$query_Recordset1 = "SELECT keyJargon FROM cma_keywords";
$Recordset1 = mysql_query($query_Recordset1, $cmaConn) or
die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

It's an associative array of the entire recordset, right, i.e., all of the
jargon words keyed to their record number?

--
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
==================


"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns98E05E10C13E5makowiecatnycapdotrE@216.104.212.96...
> On 23 Feb 2007 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:
>
>> No - I want to do it on the associative array $row_rsRecordset1, which
>> contains the jargon words that I want to find in $content.
>
> But $row_rsRecordset1 is the associative array containing everything from
> one row in the result set. An individual field from within that array
> would be $row_rsRecordset1['myTextField'].
>
> --
> Joe Makowiec
> http://makowiec.net/
> Email: http://makowiec.net/email.php


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2007 Feb 23, 2007

Copy link to clipboard

Copied

In:

$query_Recordset1 = "SELECT keyJargon FROM cma_keywords";
$Recordset1 = mysql_query($query_Recordset1, $cmaConn) or
die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

$row_Recordset1 = mysql_fetch_assoc($Recordset1); contains an associative
array of the first row of the result only, not all the results. If you add a
repeat region, you`ll see that it uses a while loop (if I remember
correctly), using mysql_fetch_assoc for each record.

So add a repeat region to the page, and then modify the code, so that it
does the replace for each record, and I`d put result in a different array
you can use elsewhere.


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


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2007 Feb 23, 2007

Copy link to clipboard

Copied

LATEST
That's it. Thanks, Gareth!

--
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:ermurl$l0u$1@forums.macromedia.com...
> In:
>
> $query_Recordset1 = "SELECT keyJargon FROM cma_keywords";
> $Recordset1 = mysql_query($query_Recordset1, $cmaConn) or
> die(mysql_error());
> $row_Recordset1 = mysql_fetch_assoc($Recordset1);
>
> $row_Recordset1 = mysql_fetch_assoc($Recordset1); contains an associative
> array of the first row of the result only, not all the results. If you add
> a repeat region, you`ll see that it uses a while loop (if I remember
> correctly), using mysql_fetch_assoc for each record.
>
> So add a repeat region to the page, and then modify the code, so that it
> does the replace for each record, and I`d put result in a different array
> you can use elsewhere.
>
>
> --
> Gareth
> http://www.phploginsuite.co.uk/
> PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.
>


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines