2012-05-26 49 views
2

我想解析以下bibtex字符串或者一個整個.bib文件使用PHP或javascript。解析中文提供文件

有什麼建議嗎?

的字符串看起來像:

@article{Sng:2010:PMW:1750585.1750678, 
author = {Sng, Colin T. S. and Manlove, David F.}, 
title = {Popular matchings in the weighted capacitated house allocation problem}, 
journal = {J. of Discrete Algorithms}, 
issue_date = {June, 2010}, 
volume = {8}, 
number = {2}, 
month = jun, 
year = {2010}, 
issn = {1570-8667}, 
pages = {102--116}, 
numpages = {15}, 
url = {http://dx.doi.org/10.1016/j.jda.2008.11.008}, 
doi = {10.1016/j.jda.2008.11.008}, 
acmid = {1750678}, 
publisher = {Elsevier Science Publishers B. V.}, 
address = {Amsterdam, The Netherlands, The Netherlands}, 
keywords = {Maximum popular matching, Polynomial-time algorithm, Popular matching  problem, Priorities, Strict preference lists}, 
} 

回答

7

我不太記得了中文提供語法,但我會建議由JSON.parse的語法,使其可解析轉換,就像這樣:

var bibjson = bibtex.replace(/(\w+)\s*=\s*\{/g,"\"$1\": \"") 
      .replace(/\}(?=\s*[,\}])/g,"\"") 
      .replace(/@(\w+)\s*\{([^,]*)/,"{\"$1\": \"$2\""); 
var bibobj = JSON.parse(bibjson); 
+0

感謝您的時間。我相信你的代碼有錯誤。我無法運行它。第一行有錯誤。 '解析錯誤:語法錯誤,意想不到的 '/',期望 ')' 在/homepages/26/d94605010/htdocs/lz/php/index.php(153):EVAL()'上線21' – glarkou

+0

d代碼我給你是一個Javascript解決方案,而不是PHP。 – MaxArt

+0

可以把它放在jsfiddle上嗎? – glarkou