Skip to content

Instantly share code, notes, and snippets.

@Bloodb0ne
Created January 20, 2015 15:06
Show Gist options
  • Save Bloodb0ne/69e840c61d011b232246 to your computer and use it in GitHub Desktop.
Save Bloodb0ne/69e840c61d011b232246 to your computer and use it in GitHub Desktop.
<?php
//Helpers
function s($text){
return "\"".trim($text," }{")."\"";
}
function m1($m){
return s($m[1]) . ":" . s($m[2]);
}
function m2($m){
return s($m[1]) . ":" . s($m[2]).",";
}
//Parse function
function bibtexToJson($filename){
$content = file_get_contents($filename);
$content = preg_replace("/\@(\w)*\{(.*),/","",$content);
$content = mb_substr($content,0,strrpos($content,"}"));
$content = preg_replace_callback("/(\\w+)\\s*=\\s*\\{([\\/\sa-zA-Z0-9.:,\}\{\-#]*)\\}/","m1", $content);
$content = preg_replace_callback("/(\w+)\s*=\s*([?!\}\/\\\sa-zA-Z0-9.:,\#]*),/","m2",$content);
return json_decode("{".$content."}",true);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment