Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

refine_searchstring()

Description

Refines the search string to eliminate duplicates and ensure proper formatting.

This function addresses several issues related to searching, including:
- Eliminating duplicate terms from the search query.
- Preserving string search functionality when quotes are used.
- Formatting date-related keywords correctly.
- Adjusting keywords for advanced search fields and ensuring they carry over properly.
- Fixing bugs related to search separators and ensuring valid search syntax.

Parameters

ColumnTypeDefaultDescription
$search string The original search string to be refined.

Return

string The refined search string, with duplicates removed and properly formatted.

Location

include/search_functions.php lines 561 to 634

Definition

 
function refine_searchstring($search)
    {
    global 
$use_refine_searchstring;

    if (!
$use_refine_searchstring){return $search;}

    if (
substr($search,0,1)=="\"" && substr($search,-1,1)=="\"") {return $search;} // preserve string search functionality.

    
global $noadd;
    
$search=str_replace(",-",", -",$search);
    
$search=str_replace ("\xe2\x80\x8b","",$search);// remove any zero width spaces.

    
$keywords=split_keywords($searchfalsefalsefalsefalsetrue);

    
$orfields=get_OR_fields(); // leave checkbox type fields alone
    
$dynamic_keyword_fields=ps_array("SELECT name value FROM resource_type_field where type=9", array(), "schema");

    
$fixedkeywords=array();
    foreach (
$keywords as $keyword)
        {
        if (
strpos($keyword,"startdate")!==false || strpos($keyword,"enddate")!==false)
            {
            
$keyword=str_replace(" ","-",$keyword);
            }

        if(
strpos($keyword,"!collection") === 0)
            {
            
$collection=intval(substr($search,11));
            
$keyword "!collection" $collection;
            }

        if (
strpos($keyword,":")>0)
            {
            
$keywordar=explode(":",$keyword,2);
            
$keyname=$keywordar[0];
            if (
substr($keyname,0,1)!="!")
                {
                if(
substr($keywordar[1],0,5)=="range"){$keywordar[1]=str_replace(" ","-",$keywordar[1]);}
                if (!
in_array($keyname,$orfields))
                    {
                    
$keyvalues=explode(" ",str_replace($keywordar[0].":","",$keywordar[1]));
                    }
                else
                    {
                    
$keyvalues=array($keywordar[1]);
                    }
                foreach (
$keyvalues as $keyvalue)
                    {
                    if (!
in_array($keyvalue,$noadd))
                        {
                        
$fixedkeywords[]=$keyname.":".$keyvalue;
                        }
                    }
                }
            elseif (!
in_array($keyword,$noadd))
                {
                
$keywords=explode(" ",$keyword);
                
$fixedkeywords[]=$keywords[0];
                } 
// for searches such as !list
            
}
        else
            {
            if (!
in_array($keyword,$noadd))
                {
                
$fixedkeywords[]=$keyword;
                }
            }
        }
    
$keywords=$fixedkeywords;
    
$keywords=array_unique($keywords);
    
$search=implode(", ",$keywords);
    
$search=str_replace(",-"," -",$search); // support the omission search
    
return $search;
    }

This article was last updated 17th November 2024 15:05 Europe/London time based on the source file dated 8th November 2024 11:45 Europe/London time.