57 lines
1.2 KiB
PHP
57 lines
1.2 KiB
PHP
<?php
|
|
class Search
|
|
{
|
|
var $terms;
|
|
var $text;
|
|
|
|
function Search($text)
|
|
{
|
|
$this->terms = array();
|
|
$this->text=$text;
|
|
}
|
|
|
|
private function safe_query($search)
|
|
{
|
|
return preg_replace('/%|_|\'|\\\\/', '\\\\$0', stripslashes($search));
|
|
}
|
|
|
|
function parse_search($search="", $safe = true)
|
|
{
|
|
if (empty($search)) $search=$this->text;
|
|
$temp = array();
|
|
preg_match_all('/"([^"]+)"|([^\\s]+)/', (( $safe ) ? $this->safe_query($search) : $search), $temp);
|
|
|
|
for ($i = 1; $i < count($temp); $i++)
|
|
{
|
|
foreach ( $temp[$i] as $value )
|
|
{
|
|
if ( strlen($value) >= 3 )
|
|
{
|
|
$this->terms[] = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
function sql_expr($field)
|
|
{
|
|
$sql=" 1=1 ";
|
|
if (!is_array($field))
|
|
{
|
|
$field=array($field);
|
|
}
|
|
if (!count($this->terms))
|
|
$this->parse_search();
|
|
foreach($this->terms as $t)
|
|
{
|
|
$sql.=" and (1<>1 ";
|
|
foreach($field as $f)
|
|
{
|
|
$sql.=" or ".$f." ilike '%".$t."%' ";
|
|
}
|
|
$sql.=") ";
|
|
}
|
|
return $sql;
|
|
}
|
|
} |