Browse Source

adjusted search highlight feature to accomodate multiple strings

main
Simon Bowie 2 years ago
parent
commit
98535248cf
3 changed files with 13 additions and 6 deletions
  1. +4
    -3
      site/public/search.php
  2. +3
    -0
      site/solr.php
  3. +6
    -3
      site/text_functions.php

+ 4
- 3
site/public/search.php View File

@@ -143,11 +143,12 @@ include '../text_functions.php';

</div>
<script>
let text = "<?php echo $_POST["search"]; ?>";
upperText = text.toUpperCase();
result = "<span style='color:orange'>" + upperText + "</span>";
const collection = document.getElementsByClassName("search_term");
for (let i = 0; i < collection.length; i++) {
let text = collection[i].textContent;
console.log(collection[i]);
upperText = text.toUpperCase();
result = "<span style='color:orange'>" + upperText + "</span>";
collection[i].innerHTML = result;
}
</script>

+ 3
- 0
site/solr.php View File

@@ -2,6 +2,9 @@

function solr_search($search, $core, $sort){

// URL encode the search term
$search = urlencode($search);

if ($sort == 'relevance'){
// Assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
$solrurl = 'http://' . $_ENV["SOLR_HOSTNAME"] . ':' . $_ENV["SOLR_PORT"] . '/solr/' . $core . '/select?q.op=OR&q=content%3A' . $search . '&wt=json';

+ 6
- 3
site/text_functions.php View File

@@ -1,8 +1,11 @@
<?php

function highlight_search_terms($array, $search_term){
$replacement = "<span class='search_term'>$search_term</span>";
$result = str_ireplace($search_term, $replacement, $array);
function highlight_search_terms($result, $search_term){
$search_array = explode(" ", $search_term);
foreach ($search_array as $term) {
$replacement = "<span class='search_term'>$term</span>";
$result = str_ireplace($term, $replacement, $result);
}
return $result;
}


Loading…
Cancel
Save