Skip to content

Commit

Permalink
Merge pull request #105 from multnomah-county-it/dev
Browse files Browse the repository at this point in the history
Added prepare_search function to clean up text for use in an ILSWS se…
  • Loading branch information
john-c-houser authored Sep 29, 2022
2 parents 49487c5 + 63d30dd commit 8eba41a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Libilsws.php
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,44 @@ public function describe_bib ($token = null)
return $this->send_get("$this->base_url/catalog/bib/describe", $token, []);
}

/**
* Removes accents, punctuation, and non-ascii characters to
* create search string acceptable to ILSWS
*
* @param string $terms
* @return string $terms
*/

public function prepare_search ($terms = null)
{
// Trim leading and trailing whitespace
$terms = trim($terms);

// Validate
$this->validate('terms', $terms, 's:256');

// Change utf8 letters with accents to ascii characters
setlocale(LC_ALL, "en_US.utf8");
$terms = iconv("utf-8", "ASCII//TRANSLIT", $terms);

// Remove boolean operators
$terms = preg_replace("/(\s+)(and|or|not)(\s+)/", ' ', $terms);

// Replace certain characters with a space
$terms = preg_replace("/[.:;?!,\/]/", ' ', $terms);

// Remove most punctuation and other unwanted characters
$terms = preg_replace("/[&+=><%\'\"\|\{\}\(\)]/", '', $terms);

// Remove internal non-printing characters
$terms = preg_replace('/[^\x20-\x7E]/','', $terms);

// Replace multiple spaces with a single space
$terms = preg_replace('/\s+/', ' ', $terms);

return $terms;
}

/**
* Search the catalog for bib records
*
Expand Down
5 changes: 5 additions & 0 deletions test/search_bib.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
// Connect and get token
$token = $ilsws->connect();

// Remove unwanted characters from search string
$search = $ilsws->prepare_search($search);

print "$search\n";

/**
* Search for a patron. If the $params array is empty or any item is omitted,
* default values will be supplied as shown, with the exception of the
Expand Down

0 comments on commit 8eba41a

Please sign in to comment.