Remove Particular Characters from Search Query String

After switching all database tables from MyISAM to InnoDB, you will want to prevent particular characters as ending character and ( ) or — in search query to prevent query error

This mod will prevent particular characters as ending character of the search term

This works for both normal search and quick AJAX search.

In /includes/classes/class.search.php

Find (for normal search)

if (isset($altData['search_query'])) {

Add after

//===========================
// MOD START
// remove + and - from ending character
//===========================
$lc = substr(trim($altData['search_query']), -1);
if($lc == '-' || $lc == '+'){
 $altData['search_query'] = substr(trim($altData['search_query']), 0, -1);
}
$altData['search_query'] = str_replace("(", "", $altData['search_query']);
$altData['search_query'] = str_replace(")", "", $altData['search_query']);
$altData['search_query'] = str_replace("--", "-", $altData['search_query']);
// MOD END
//===========================

Find (for AJAX quick search)

foreach ($searchTypes as $searchType) {

Add after

//===========================
// MOD START
// remove + and - from ending character
//===========================
$lc = substr(trim($this->_searchterms['search_query']), -1);
if($lc == '-' || $lc == '+'){
	$this->_searchterms['search_query'] = substr(trim($this->_searchterms['search_query']), 0, -1);
}
// MOD END
//===========================

Leave a Reply

Your email address will not be published. Required fields are marked *