You are here:  » Highlighting search term word in results

Support Forum



Highlighting search term word in results

Submitted by webie on Sun, 2007-12-16 01:44 in

Hi Dave,

is it possible to high light the search term in the returned search results
example if i search for mig welder and we get the code to apply bold tag to
to search term mig welder. for every result displyed?

cheers

Darren

Submitted by support on Sun, 2007-12-16 11:32

Hi Darren,

First thing to do I think is to create a function in one of the include files (I suggest includes/tapestry.php that will take 2 parameters ($text and $words) and then add SPAN tags with a CLASS value to each word from $words within $text. This is because the product name is already displayed in bold type, so by using a SPAN you can then control the appearance of the highlight by adding a style for the .hl class to default.css. Here's the function:

  function tapestry_highlight($text,$words)
  {
    $words = explode(" ",$words);
    foreach($words as $word)
    {
      $word = strtolower($word);
      // UPPERCASE
      $text = str_replace(strtoupper($word),"<span class='hl'>".strtoupper($word)."</span>",$text);
      // Namecase
      $text = str_replace(ucwords($word),"<span class='hl'>".ucwords($word)."</span>",$text);
      // lowercase
      $text = str_replace($word,"<span class='hl'>".$word."</span>",$text);
    }
    return $text;
  }

To use it, simply modify html/searchresults.php, and replace each instance of:

$product["name"]

...with:

tapestry_highlight($product["name"],$q)

Finally, to add the appropriate style to default.css, try something like:

.hl {
  background-color: yellow;
}

(although you will probably want to make it less subtle than that!)

Cheers,
David.

Submitted by webie on Sun, 2007-12-16 17:27

Hi Dave,

Excellent worked a treat thank you again for such great support buying your software was the best thing i have done worth every penny and more.

cheers once again

Darren

Submitted by nosferatu on Sun, 2008-02-10 14:11

Hi,

There is a problem with this code.

When you try to search for example a word like: sony foto dslr a 100 it make something like this:

an class='hl'>Cameraan> an class='hl'>Fotoan> an class='hl'>Profesionalaan> an class='hl'>Sonyan> an class='hl'>DSLRan> an class='hl'>an class='hl'>Aan>an> 100 H Body an class='hl'>an class='hl'>Aan>an> 100 obiectiv San class='hl'>an class='hl'>Aan>an>L 18 200

I will be grateful if you can help me,

Thanks

Submitted by support on Sun, 2008-02-10 19:20

Hi,

I can see why this is happening. When the HTML has been inserted, the search and replace for the word "a" is replacing the "a" in "span"....!

To get around this, the trick is to replace with a discreet keyword instead of the HTML, and then replace in the HTML after all replacements have been done. Try this version of the function:

  function tapestry_highlight($text,$words)
  {
    $words = explode(" ",$words);
    foreach($words as $word)
    {
      $word = strtolower($word);
      // UPPERCASE
      $text = str_replace(strtoupper($word),"[[".strtoupper($word)."]]",$text);
      // Namecase
      $text = str_replace(ucwords($word),"[[".ucwords($word)."]]",$text);
      // lowercase
      $text = str_replace($word,"[[".$word."]]",$text);
    }
    $text = str_replace("[[","<span class='hl'>",$text);
    $text = str_replace("]]","</span>",$text);
    return $text;
  }

A little more modification may be required in order to search for words only (i.e. adding spaces to the beginning and end of each word) - let me know if this is the case...

Hope this helps!
Cheers,
David.

Submitted by nosferatu on Sun, 2008-02-10 20:48

Hi David,

Thanks(again)verry much, it works great now.

Submitted by webie on Fri, 2008-02-22 02:57

Hi Dave,

Is it possiable to include stopword.txt file for the code not to highlight words like a,for,our etc

cheers

Darren

Submitted by support on Fri, 2008-02-22 09:04

Hi Darren,

It's an easy mod to the highlight function. Here's an alternative version:

  function tapestry_highlight($text,$words)
  {
    $stopwords = array("a"=>1,"for"=>1,"our"=>1);
    $words = explode(" ",$words);
    foreach($words as $word)
    {
      if ($stopwords[$word]) continue;
      $word = strtolower($word);
      // UPPERCASE
      $text = str_replace(strtoupper($word),"[[".strtoupper($word)."]]",$text);
      // Namecase
      $text = str_replace(ucwords($word),"[[".ucwords($word)."]]",$text);
      // lowercase
      $text = str_replace($word,"[[".$word."]]",$text);
    }
    $text = str_replace("[[","<span class='hl'>",$text);
    $text = str_replace("]]","</span>",$text);
    return $text;
  }

To add more or change the stopwords, just edit this line:

    $stopwords = array("a"=>1,"for"=>1,"our"=>1);

This means that the script doesn't have to read a text file on every page view...

Hope this helps!

Cheers,
David.

Submitted by webie on Sat, 2008-02-23 19:08

Hi Dave,

Once again fanstatic support work a treat.

thank again

all the best

Darren

Submitted by webie on Sat, 2008-02-23 22:32

Hi Dave,

Just created an array like you said from stopword list how large can array be I have over hundred words is way too many for the array it stop word list from the web ?

cheers

Darren

Submitted by webie on Sat, 2008-02-23 22:46

Hi Dave

Sorry found the error forgot comma between one of array words.

Here is my array do think this way too much for stopword list?

Cheers Darren

<?php
$stopwords 
= array("a"=>1,"about"=>1,"above"=>1,"across"=>1,"after"=>1,"again"=>1,"against"=>1,
"all"=>1,"almost"=>1,"alone"=>1,"along"=>1,"already"=>1,"also"=>1,"although"=>1,"always"=>1,
"among"=>1,"an"=>1,"and"=>1,"another"=>1,"any"=>1,"anybody"=>1,"anyone"=>1,"anything"=>1,
"anywhere"=>1,"are"=>1,"area"=>1,"areas"=>1,"around"=>1,"as"=>1,"ask"=>1,"asked"=>1,
"asking"=>1,"asks"=>1,"at"=>1,"away"=>1,"back"=>1,"backed"=>1,"backing"=>1,"backs"=>1,
"be"=>1,"became"=>1,"because"=>1,"become"=>1,"becomes"=>1,"been"=>1,"before"=>1,"began"=>1,
"behind"=>1,"being"=>1,"beings"=>1,"best"=>1,"better"=>1,"between"=>1,"big"=>1,"both"=>1,
"but"=>1,"by"=>1,"came"=>1,"can"=>1,"cannot"=>1,"case"=>1,"cases"=>1,"certain"=>1,"certainly"=>1,
"clear"=>1,"clearly"=>1,"come"=>1,"could"=>1,"did"=>1,"differ"=>1,"different"=>1,"differently"=>1,
"do"=>1,"does"=>1,"done"=>1,"down"=>1,"down"=>1,"downed"=>1,"downing"=>1,"downs"=>1,"during"=>1,
"each"=>1,"early"=>1,"either"=>1,"end"=>1,"ended"=>1,"ending"=>1,"ends"=>1,"enough"=>1,"even"=>1,
"evenly"=>1,"ever"=>1,"every"=>1,"everybody"=>1,"everyone"=>1,"everything"=>1,"everywhere"=>1,
"face"=>1,"faces"=>1,"fact"=>1,"facts"=>1,"far"=>1,"felt"=>1,"few"=>1,"find"=>1,"finds"=>1,
"first"=>1,"for"=>1,"four"=>1,"from"=>1,"full"=>1,"fully"=>1,"further"=>1,"furthered"=>1,
"furthering"=>1,"furthers"=>1,"gave"=>1,"general"=>1,"generally"=>1,"get"=>1,"gets"=>1,
"give"=>1,"given"=>1,"gives"=>1,"go"=>1,"going"=>1,"good"=>1,"goods"=>1,"got"=>1,"great"=>1,
"greater"=>1,"greatest"=>1,"group"=>1,"grouped"=>1,"grouping"=>1,"groups"=>1,"had"=>1,"has"=>1,
"have"=>1,"having"=>1,"he"=>1,"her"=>1,"here"=>1,"herself"=>1,"high"=>1,"high"=>1,"high"=>1,"higher"=>1,
"highest"=>1,"him"=>1,"himself"=>1,"his"=>1,"how"=>1,"however"=>1,"i"=>1,"if"=>1,"important"=>1,"in"=>1,
"interest"=>1,"interested"=>1,"interesting"=>1,"interests"=>1,"into"=>1,"is"=>1,"it"=>1,"its"=>1,
"itself"=>1,"just"=>1,"keep"=>1,"keeps"=>1,"kind"=>1,"knew"=>1,"know"=>1,"known"=>1,"knows"=>1,
"large"=>1,"largely"=>1,"last"=>1,"later"=>1,"latest"=>1,"least"=>1,"less"=>1,"let"=>1,"lets"=>1,
"like"=>1,"likely"=>1,"long"=>1,"longer"=>1,"longest"=>1,"made"=>1,"make"=>1,"making"=>1,"man"=>1,
"many"=>1,"may"=>1,"me"=>1,"member"=>1,"members"=>1,"men"=>1,"might"=>1,"more"=>1,"most"=>1,"mostly"=>1,
"mr"=>1,"mrs"=>1,"much"=>1,"must"=>1,"my"=>1,"myself"=>1,"necessary"=>1,"need"=>1,"needed"=>1,"needing"=>1,
"needs"=>1,"never"=>1,"new"=>1,"new"=>1,"newer"=>1,"newest"=>1,"next"=>1,"no"=>1,"nobody"=>1,"non"=>1,
"noone"=>1,"not"=>1,"nothing"=>1,"now"=>1,"nowhere"=>1,"number"=>1,"numbers"=>1,"of"=>1,"off"=>1,
"often"=>1,"old"=>1,"older"=>1,"oldest"=>1,"on"=>1,"once"=>1,"one"=>1,"only"=>1,"open"=>1,"opened"=>1,
"opening"=>1,"opens"=>1,"or"=>1,"order"=>1,"ordered"=>1,"ordering"=>1,"orders"=>1,"other"=>1,"others"=>1,
"our"=>1,"out"=>1,"over"=>1,"part"=>1,"parted"=>1,"parting"=>1,"parts"=>1,"per"=>1,"perhaps"=>1,"place"=>1,
"places"=>1,"point"=>1,"pointed"=>1,"pointing"=>1,"points"=>1,"possible"=>1,"present"=>1,"presented"=>1,
"presenting"=>1,"presents"=>1,"problem"=>1,"problems"=>1,"put"=>1,"puts"=>1,"quite"=>1,"rather"=>1,
"really"=>1,"right"=>1,"room"=>1,"rooms"=>1,"said"=>1,"same"=>1,"saw"=>1,"say"=>1,"says"=>1,
"second"=>1,"seconds"=>1,"see"=>1,"seem"=>1,"seemed"=>1,"seeming"=>1,"seems"=>1,"sees"=>1,"several"=>1,
"shall"=>1,"she"=>1,"should"=>1,"show"=>1,"showed"=>1,"showing"=>1,"shows"=>1,"side"=>1,"sides"=>1,"since"=>1,
"small"=>1,"smaller"=>1,"smallest"=>1,"so"=>1,"some"=>1,"somebody"=>1,"someone"=>1,"something"=>1,"somewhere"=>1,
"state"=>1,"states"=>1,"still"=>1,"still"=>1,"such"=>1,"sure"=>1,"take"=>1,"taken"=>1,"than"=>1,"that"=>1,
"the"=>1,"their"=>1,"them"=>1,"then"=>1,"there"=>1,"therefore"=>1,"these"=>1,"they"=>1,"thing"=>1,"things"=>1,
"think"=>1,"thinks"=>1,"this"=>1,"those"=>1,"though"=>1,"thought"=>1,"thoughts"=>1,"three"=>1,"through"=>1,
"thus"=>1,"to"=>1,"today"=>1,"together"=>1,"too"=>1,"took"=>1,"toward"=>1,"turn"=>1,"turned"=>1,"turning"=>1,
"turns"=>1,"two"=>1,"under"=>1,"until"=>1,"up"=>1,"upon"=>1,"us"=>1,"use"=>1,"used"=>1,"uses"=>1,"very"=>1,
"want"=>1,"wanted"=>1,"wanting"=>1,"wants"=>1,"was"=>1,"way"=>1,"ways"=>1,"we"=>1,"well"=>1,"wells"=>1,"went"=>1,
"were"=>1,"what"=>1,"when"=>1,"where"=>1,"whether"=>1,"which"=>1,"while"=>1,"who"=>1,"whole"=>1,"whose"=>1,
"why"=>1,"will"=>1,"with"=>1,"within"=>1,"without"=>1,"work"=>1,"worked"=>1,"working"=>1,"works"=>1,"would"=>1,
"year"=>1,"years"=>1,"yet"=>1,"you"=>1,"young"=>1,"younger"=>1,"youngest"=>1,"your"=>1,"yours"=>1);
?>

Submitted by support on Sun, 2008-02-24 10:52

Hi Darren,

That's fine - the stopwords function is only ever called once, so there is no performance gain by doing it any other way!

Cheers,
David.