| 
	
 | 
 Posted by Juha Suni on 09/07/05 17:28 
brendan wrote: 
> I need to break a search string into components to search against a 
> database. 
> We want to allow the user to specify database columns by using 
> 
> [column name][colon][search text] 
> 
> i.e. 
> "author:Jane Smith institution:university of Cambridge year:1976" 
 
Not regexp, more quick'n'dirty but perfectly works and accepts strings in  
the format above with as many parameters as required. 
 
All the data is available in a single array ($querydata), see the bottom of  
the script: 
 
 
$string = "author:Jane Smith institution:university of Cambridge year:1976"; 
 
$qrykeys = array(); 
$qryvalues = array(); 
$tmpwords = array(); 
 
$parts = explode(" ",$string); 
 
foreach($parts as $key => $value) { 
 if (strpos($value,":") !== false) { 
  if (count($tmpwords) >= 1) { 
   array_push($qryvalues,implode(" ",$tmpwords)); 
   $tmpwords = array(); 
   } 
  $subparts = explode(":",$value); 
  array_push($qrykeys,$subparts[0]); 
  array_push($tmpwords,$subparts[1]); 
  } 
 else { 
  array_push($tmpwords,$value); 
  } 
 } 
 
array_push($qryvalues,implode(" ",$tmpwords)); 
$tmpwords = array(); 
 
$querydata = array(); 
for ($i = 0; $i!=count($qrykeys) ; $i++) { 
 $querydata[$qrykeys[$i]] = $qryvalues[$i]; 
 } 
 
print_r($querydata); 
 
 
 
 
HTH 
--  
Suni
 
  
Navigation:
[Reply to this message] 
 |