Post by letxob » Fri Aug 18, 2017 3:35 am

Highlight search keywords in your product page:
(used to write this for OSCommerce).

1) add these functions to your catalog/model/catalog/product.php

public function HighlightViewableText($text, $keywords, $class) {

if (($text == '') or ($keywords == '')) return $text;

$keywords = str_replace('#', '', $keywords);

if (!($check = $this->ParseSearchString($keywords, $search_keywords))) return $text;

$strip_array = array('and', 'or', 'span', 'font', 'table', 'body','textarea', 'script');
$result = $text;
$cli = 0;

for ($i = 0; $i<sizeof($search_keywords); $i++) {
if (!in_array(UTF8_strtolower($search_keywords[$i]), $strip_array)) {
if ($cli == 0) { $cln = '';} else { $cln = $cli;}
$result = $this->HighlightText($result, $search_keywords[$i], $class . $cln);
$cli++;
if ($cli > 2) {$cli = 0;}
}
}
return $result;
}

public function HighlightText($text, $token, $class) {

$text = preg_replace('/(?!<.*?)(' . preg_quote($token, '/') . ')(?![^<>]*?>)/si','<span class="' . $class . '">' . $token . '</span>',$text);
return $text;
}

public function ParseSearchString($search_str = '', &$objects) {

$search_str = trim(UTF8_strtolower($search_str));

// Break up $search_str on whitespace; quoted string will be reconstructed later
$pieces = split('[[:space:]]+', $search_str);
$objects = array();
$tmpstring = '';
$flag = '';

for ($k=0; $k<count($pieces); $k++) {
while (substr($pieces[$k], 0, 1) == '(') {
$objects[] = '(';
if (strlen($pieces[$k]) > 1) {
$pieces[$k] = substr($pieces[$k], 1);
} else {
$pieces[$k] = '';
}
}

$post_objects = array();

while (substr($pieces[$k], -1) == ')') {
$post_objects[] = ')';
if (strlen($pieces[$k]) > 1) {
$pieces[$k] = substr($pieces[$k], 0, -1);
} else {
$pieces[$k] = '';
}
}

// Check individual words

if ( (substr($pieces[$k], -1) != '"') && (substr($pieces[$k], 0, 1) != '"') ) {
$objects[] = trim($pieces[$k]);

for ($j=0; $j<count($post_objects); $j++) {
$objects[] = $post_objects[$j];
}
} else {
/* This means that the $piece is either the beginning or the end of a string.
So, we'll slurp up the $pieces and stick them together until we get to the
end of the string or run out of pieces.
*/

// Add this word to the $tmpstring, starting the $tmpstring
$tmpstring = trim(ereg_replace('"', ' ', $pieces[$k]));

// Check for one possible exception to the rule. That there is a single quoted word.
if (substr($pieces[$k], -1 ) == '"') {
// Turn the flag off for future iterations
$flag = 'off';

$objects[] = trim($pieces[$k]);

for ($j=0; $j<count($post_objects); $j++) {
$objects[] = $post_objects[$j];
}

unset($tmpstring);

// Stop looking for the end of the string and move onto the next word.
continue;
}

// Otherwise, turn on the flag to indicate no quotes have been found attached to this word in the string.
$flag = 'on';

// Move on to the next word
$k++;

// Keep reading until the end of the string as long as the $flag is on

while ( ($flag == 'on') && ($k < count($pieces)) ) {
while (substr($pieces[$k], -1) == ')') {
$post_objects[] = ')';
if (strlen($pieces[$k]) > 1) {
$pieces[$k] = substr($pieces[$k], 0, -1);
} else {
$pieces[$k] = '';
}
}

// If the word doesn't end in double quotes, append it to the $tmpstring.
if (substr($pieces[$k], -1) != '"') {
// Tack this word onto the current string entity
$tmpstring .= ' ' . $pieces[$k];

// Move on to the next word
$k++;
continue;
} else {
/* If the $piece ends in double quotes, strip the double quotes, tack the
$piece onto the tail of the string, push the $tmpstring onto the $haves,
kill the $tmpstring, turn the $flag "off", and return.
*/
$tmpstring .= ' ' . trim(ereg_replace('"', ' ', $pieces[$k]));

// Push the $tmpstring onto the array of stuff to search for
$objects[] = trim($tmpstring);

for ($j=0; $j<count($post_objects); $j++) {
$objects[] = $post_objects[$j];
}

unset($tmpstring);

// Turn off the flag to exit the loop
$flag = 'off';
}
}
}
}

// add default logical operators if needed
$temp = array();
for($i=0; $i<(count($objects)-1); $i++) {
$temp[] = $objects[$i];
if ( ($objects[$i] != 'and') &&
($objects[$i] != 'or') &&
($objects[$i] != '(') &&
($objects[$i+1] != 'and') &&
($objects[$i+1] != 'or') &&
($objects[$i+1] != ')') ) {
$temp[] = 'and';
}
}
$temp[] = $objects[$i];
$objects = $temp;

$keyword_count = 0;
$operator_count = 0;
$balance = 0;
for($i=0; $i<count($objects); $i++) {
if ($objects[$i] == '(') $balance --;
if ($objects[$i] == ')') $balance ++;
if ( ($objects[$i] == 'and') || ($objects[$i] == 'or') ) {
$operator_count ++;
} elseif ( ($objects[$i]) && ($objects[$i] != '(') && ($objects[$i] != ')') ) {
$keyword_count ++;
}
}



if ( ($operator_count < $keyword_count) && ($balance == 0) ) {
return true;
} else {
return false;
}
}






2) add these definitions to your CSS file(s) different background colors for up to 5 keywords.


SPAN.searchTag {color: white; background-color: #F599A6;}
SPAN.searchTag2 {color: white; background-color: #4BCB66;}
SPAN.searchTag1 {color: white; background-color: #6fbaf1;}
SPAN.searchTag3 {color: white; background-color: #C8D224;}
SPAN.searchTag4 {color: white; background-color: #ED7EE7;}





3) change these calls in your catalog/controller/product/product.php


// if you want the product name highlighted
$data['name'] = $this->model_catalog_product->HighlightViewableText($product_info['name'], $this->request->get['search'], 'searchTag');

// description also
$data['description'] = $this->model_catalog_product->HighlightViewableText($data['description'], $this->request->get['search'], 'searchTag');

Newbie

Posts

Joined
Sun Jul 02, 2017 4:36 am
Who is online

Users browsing this forum: No registered users and 30 guests