arrr, that was tougher than I thought... I can respect pointing to the regex manual and being done with it, but I was bored...
I broke it up into two regexs, may not be the most efficient, but seems to work.
Code: Select all
<?php
$string = '@article{Gettys90,
author = {Jim Gettys and Phil Karlton and Scott McGregor},
title = {The {X} Window System, Version 11},
journal = {Software Practice and Experience},
volume = {20},
number = {S2},
year = {1990},
abstract = {A technical overview of the X11 functionality. This is an update
of the X10 TOG paper by Scheifler \& Gettys.}
}
@article{Gettys07,
author = {Jim Gettys and Phil Karlton and Scott McGregor},
title = {The {X} Window System, Version 12},
journal = {Software Practice and Experience},
volume = {21},
number = {S3},
year = {2007},
abstract = {A technical overview of the X12 functionality. This is an update
of the X11 TOG paper by Scheifler \& Gettys.}
}';
// matches each @whatever putting the type in sub1, id in sub2, and all of the attributes including the trailing } in sub3
$pattern = "/@([0-9a-z]*){([0-9a-z]*)\s*,(.*})\s*}/siU";
preg_match_all($pattern, $string, $matches);
for ($i = 0; $i < count($matches[0]); $i++) {
$output[$i]["type"] = $matches[1][$i];
$output[$i]["id"] = $matches[2][$i];
// matches each attribute putting the name in sub1 and the value in sub2
$pattern2 = "/\s*([0-9a-z]*)\s*=\s*{(.*)},/siU";
// I add a comma the end of the attributes so they all end with },
// Note I make sure to capture the second to last } in the previous regex.
preg_match_all($pattern2, $matches[3][$i] . ",", $matches2);
for ($j = 0; $j < count($matches2[0]); $j++) {
$output[$i][$matches2[1][$j]] = $matches2[2][$j];
}
}
print_r($output);
?>
Outputs:
Code: Select all
Array
(
[0] => Array
(
[type] => article
[id] => Gettys90
[author] => Jim Gettys and Phil Karlton and Scott McGregor
[title] => The {X} Window System, Version 11
[journal] => Software Practice and Experience
[volume] => 20
[number] => S2
[year] => 1990
[abstract] => A technical overview of the X11 functionality. This is an update
of the X10 TOG paper by Scheifler \& Gettys.
)
[1] => Array
(
[type] => article
[id] => Gettys07
[author] => Jim Gettys and Phil Karlton and Scott McGregor
[title] => The {X} Window System, Version 12
[journal] => Software Practice and Experience
[volume] => 21
[number] => S3
[year] => 2007
[abstract] => A technical overview of the X12 functionality. This is an update
of the X11 TOG paper by Scheifler \& Gettys.
)
)
May need some tweaking if more than just numbers and letters can be in the id and attribute names.