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 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]


Удаленная работа для программистов  •  Как заработать на 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

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