|
Posted by Sandman on 09/07/05 20:37
In article <dfmtmo$2ih$1@gemini.csx.cam.ac.uk>,
"brendan" <brendan_NOSPAM_@srl.cam.ac.uk> wrote:
> "Sandman" <mr@sandman.net> wrote in message
> news:mr-ECC27F.16230007092005@individual.net...> #!/usr/bin/php
> > <?
> > $string = "author:Jane Smith institution:university of Cambridge
> year:1976";
> > $m = preg_split("/\s*(\w+):/", $string, -1,
> > PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
> > for ($i=0;$i<=count($m)-1;$i+=2){
> > $sql_query[] = $m[$i] . " = '" . $m[$i+1] . "'";
> > }
> > print "select * from database.table where " . join(" and ",
> $sql_query);
> > ?>
> >
> > The output:
> >
> > select * from database.table where author = 'Jane Smith' and institution =
> > 'university of Cambridge' and year = '1976'
>
> > Sandman[.net]
>
> Thank you sandman! That works perfectly. Beer waiting at the Eagle for you!
> regards,
An even more effective way:
#!/usr/bin/php
<?
$string = "author:Jane Smith institution:university of Cambridge year:1976";
$match = preg_split("/ (?=\w+:)/", $string, -1, PREG_SPLIT_DELIM_CAPTURE)
foreach ($match as $m){
list ($field, $value) = split(":", $m);
$sql_query[] = "$field = '$value'";
}
print "select * from database.table where " . join(" and ", $sql_query);
?>
And I don't drink alcohol :)
--
Sandman[.net]
[Back to original message]
|