[pmwiki-users] Page Text Variables Array . . .
Ben Wilson
dausha at gmail.com
Thu May 10 06:39:50 CDT 2007
On 5/9/07, Patrick R. Michaud <pmichaud at pobox.com> wrote:
[...]
>
> I'm going to wait on this particular functionality (page variables
> as arrays) simply because I want to think about the longer-term
> ramifications a bit. It may also be necessary at some point to
> be able to deal with CSV strings, instead of a simple split on
> commas, so that things like
[...]
Alright. I agree that allowing CSV should be possible, and it has the
advantage of allowing multiple possible uses. The following function
should implement the CSV standard.[1] I added this function to the
recipe I use, added a quoted value "Free, Beer", and the routine
properly separated the values. I leave it to you to determine its
utility and longer-reaching ramifications. :-)
function PageVarA($pn,$var,$d=',',$q='"') {
$fields = array();
if(is_string($d) && is_string($q)) {
$oD = '\x' . dechex(ord($d));
$oQ = '\x' . dechex(ord($q));
$re_Q = "/^$oQ((?:[^$oQ]|(?<=\\x5c)$oQ)*)$oQ$oD?(.*)$/";
$re_D = "/^([^$oD]*)$oD?(.*)$/";
$raw = PageVar($pn, $var);
while(strlen($raw) > 0) {
if($raw{0} == $q) {
preg_match($re_Q, $raw, $m);
$fields[] = str_replace('\\' . $q, $q, $m[1]);
} else {
preg_match($re_D, $raw, $m);
$fields[] = $m[1];
}
$raw = trim($m[2]);
}
}
else {
$fields[] = PageVar($pn, $var);
}
return $fields;
}
[1]: I obtained the basic functionality from a comment at
http://us.php.net/fgetcsv and streamlined the code a bit.
--
Ben Wilson
"Words are the only thing which will last forever" Churchill
More information about the pmwiki-users
mailing list