fork download
  1. <?php
  2.  
  3. $input = '@article{refregier2003weak,
  4. title={Weak gravitational lensing by large-scale structure},
  5. author={Refregier, Alexandre},
  6. journal={Annual Review of Astronomy and Astrophysics},
  7. volume={41},
  8. pages={645},
  9. year={2003},
  10. publisher={Annual Reviews, Inc.}
  11. }';
  12. $pattern = '@(\w+)=\{(.*)\}@';
  13. $articleIdPattern = '|@article\{(.*?),|';
  14. preg_match_all($pattern, $input, $matches);
  15. preg_match_all($articleIdPattern, $input, $articleMatches);
  16.  
  17. $result = [];
  18. if (isset($matches[1]) && isset($matches[2])) {
  19. foreach ($matches[1] as $key => $value) {
  20. if (isset ($matches[2][$key])) {
  21. $result[$value] = $matches[2][$key];
  22. }
  23. }
  24. }
  25. if (isset($articleMatches[1][0])) {
  26. $result['article'] = $articleMatches[1][0];
  27. }
  28.  
  29. var_dump($result);
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
array(8) {
  ["title"]=>
  string(51) "Weak gravitational lensing by large-scale structure"
  ["author"]=>
  string(20) "Refregier, Alexandre"
  ["journal"]=>
  string(43) "Annual Review of Astronomy and Astrophysics"
  ["volume"]=>
  string(2) "41"
  ["pages"]=>
  string(3) "645"
  ["year"]=>
  string(4) "2003"
  ["publisher"]=>
  string(20) "Annual Reviews, Inc."
  ["article"]=>
  string(17) "refregier2003weak"
}