|
Posted by Chung Leong on 09/07/05 17:37
That is actually a very tricky regexp to write, because the presence of
the colon at the end of a word changes its meaning, from being part of
the previous values to that of a keyword. With the example given, it'd
be hard to keep the regexp engine from appending "institution" to "Jane
Smith."
The trick here is to reverse the string, then reverse the values after
they've been extracted. Since keywords don't contain white-spaces, it's
easy to tell the regexp where it should stop.
$s = "author:Jane Smith institution:university of Cambridge year:1976";
$r = strrev($s);
preg_match_all("/\s*(.*?)\s*:\s*(\S+)/", $r, $m, PREG_SET_ORDER);
foreach($m as $set) {
$key = strrev($set[2]);
$value = strrev($set[1]);
$values[$key] = $value;
}
print_r($values);
Navigation:
[Reply to this message]
|