| 
	
 | 
 Posted by Erwin Moller on 09/07/05 15:27 
brendan wrote: 
 
> my regular expression knowledge is admittedly amateurish ... I have spent 
> an hour trying to get this to work without any great success, but am in 
> the middle of a big project so I can't waste a day fiddling around to get 
> it to work (although I might have to if someone here can't help). 
>  
> I thought I would throw it out to the group to see if anyone could solve 
> the problem quick and easy. 
>  
> 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" 
>  
> so basically we have to split the string at the beginning of any word that 
> ends in a colon. 
> I need then get an array with column_name=>column_value to construct a SQL 
> query from. 
>  
> At the moment I'm just getting garbage or parse errors ... Can anyone 
> help? Accolades for anyone that can. A pint of lager for anyone in 
> Cambridge that can! 
> cheers 
> Brendan. 
 
 
Hi, 
 
Who needs regexp? :P 
 
Try something like this: 
 
<? 
// [column name][colon][search text] 
 
$test = "author:Jane Smith institution:university of Cambridge year:1976"; 
$result = array(); 
 
$parts = explode(":",$test); 
for($i=0;$i<count($parts);$i+=2){ 
        $result[$parts[$i]] = $parts[$i+1];      
} 
 
echo "<pre>".var_dump($result)."</pre>"; 
?> 
 
 
Regards, 
Erwin Moller 
 
PS: Does your reward also include the ticket to Cambridge from Holland? ;-)
 
  
Navigation:
[Reply to this message] 
 |