You are here: Re: who is good at regular expressions? « PHP Programming Language « IT news, forums, messages
Re: who is good at regular expressions?

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]

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация