westdc-zf1/application/models/SimpleSearch.php

57 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2013-03-06 08:06:29 +00:00
<?php
2013-03-06 08:43:38 +00:00
class SimpleSearch
2013-03-06 08:06:29 +00:00
{
var $terms;
var $text;
2013-03-06 08:43:38 +00:00
function __construct($text)
2013-03-06 08:06:29 +00:00
{
$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;
}
}