2013-02-28 4 views
2

텍스트 파일에 Bibtex 내보내기가 있습니다. 텍스트 파일에는 아래 패턴에 따라 여러 항목이 있습니다.Bibtex php preg_match_all

@article{ls_leimeister, 
    added-at = {2013-01-18T11:14:11.000+0100}, 
    author = {Wegener, R. and Leimeister, J. M.}, 
    biburl = {http://www.bibsonomy.org/bibtex/27bb26b4b4858439f81aa0ec777944ac5/ls_leimeister}, 
    journal = {International Journal of Technology Enhanced Learning (to appear)}, 
    keywords = {Challenges Communities: Factors Learning Success VirtualCommunity and itegpub pub_jml pub_rwe}, 
    note = {JML_390}, 
    title = {Virtual Learning Communities: Success Factors and Challenges}, 
    year = 2013 
} 

내가 PHP를 사용하려는 다음을 didnt 어디서나 저를 얻을으로, preg_match_all

고려 :

preg_match_all('/@^.*}$/', file_get_contents($file_path),$results); 

내가 간단하게 시작하고 싶어하지만 didnt가 정말 작동합니다. 나는 RegEx에 좀 익숙하다.

완벽한 최종 출력은 다음과 같습니다

Array 
    (
     [0] => Array 
      (
       ['type'] => article 
       ['unique_name'] => ls_leimeister 
       ['added-at'] => 2013-01-18T11:14:11.000+0100 
       ['author'] => Wegener, R. and Leimeister, J. M. 
       ['biburl'] => http://www.bibsonomy.org/bibtex/27bb26b4b4858439f81aa0ec777944ac5/ls_leimeister 
       ['journal'] => International Journal of Technology Enhanced Learning (to appear) 
       ['keywords'] => Challenges Communities: Factors Learning Success VirtualCommunity and itegpub pub_jml pub_rwe 
       ['note'] => JML_390 
       ['title'] => Virtual Learning Communities: Success Factors and Challenges 
       ['year'] => 2013 
      ) 

     [1] => Array 
      (
       [...] => … 
      ) 

    ) 
+0

@ renanbr [renanbr] (https://stackoverflow.com/users/5249251/renanbr) 권장 사항 : renanbr/bibtex-parser https://github.com/renanbr/bibtex-parser 발명). – mickmackusa

+0

내가 본 BibTex 문서에는 모두 올해 값이 중괄호로 묶여 있습니다. 게시하는 동안 오타가 있습니까? – mickmackusa

답변

2

이 시도 : 여기 만 typeunique_name 가져왔다, 그것을보고, 당신은 모든 다른 사람을 가져올 수 있습니다.

$str = '@article{ls_leimeister, 
    added-at = {2013-01-18T11:14:11.000+0100}, 
    author = {Wegener, R. and Leimeister, J. M.}, 
    biburl = {http://www.bibsonomy.org/bibtex/27bb26b4b4858439f81aa0ec777944ac5/ls_leimeister}, 
    journal = {International Journal of Technology Enhanced Learning (to appear)}, 
    keywords = {Challenges Communities: Factors Learning Success VirtualCommunity and itegpub pub_jml pub_rwe}, 
    note = {JML_390}, 
    title = {Virtual Learning Communities: Success Factors and Challenges}, 
    year = 2013 
}'; 

preg_match_all('/@(?P<type>\w+){(?P<unique_name>\w+),(.*)/',$str,$matches); 

echo $matches['type'][0]; 
echo "<br>"; 
echo $matches['unique_name'][0]; 
echo "<br>"; 

echo "<pre>"; 
print_r($matches); 

출력 배열 형식은 당신에서 약간 다를 수 있습니다,하지만 당신은 당신이 형식을 변경할 수 있습니다.

+0

고마워,하지만 다른 라인은 더 diffcult 것들입니다. 줄 수는 가변적이며 일부 줄에는 '{...}'이 있으며 다른 줄은 사용하지 않습니다. – Spurious

+0

예, 어렵다는 것을 알고 있지만 그것을하려고합니다. –

+0

preg_match_all ('/ @ (\ w +) {(. +), \ s + (\ S +) \ s + = \ s + {(. *) /', $ file_content, $ results); 이렇게하면 첫 번째 줄도 생성됩니다.RegEx가 동일한 형식의 라인을 무한정 검색하는 것을 어떻게 알 수 있습니까? 항목에 대한 일치 항목을 읽고 다른 일치 항목에 대해 또 다른 preg_match를 수행해야합니다. – Spurious

0

패턴 : /^@([^{]+)\{([^,]+),\s*$|^\s*([^\[email protected]=]+) = \{(.*?)}/ms (Demo)

이 패턴은 두 가지 대안이있다; 각각은 두 개의 캡처 그룹을 포함합니다.

  • typeunique_name 캡처 및 요소와 [1][2]에 저장된다.
  • 다른 모든 키 - 값 쌍은 [3][4] 요소에 저장됩니다.

이 분리 된 어레이 스토리지는 원하는 출력 배열 구조를 구축 할 때 신뢰할 수있는 처리를 허용합니다.

입력 :

$bibtex='@BOOK{ko, 
    title = {Wissenschaftlich schreiben leicht gemacht}, 
    publisher = {Haupt}, 
    year = {2011}, 
    author = {Kornmeier, M.}, 
    number = {3154}, 
    series = {UTB}, 
    address = {Bern}, 
    edition = {4}, 
    subtitle = {für Bachelor, Master und Dissertation} 
} 

@BOOK{nial, 
    title = {Wissenschaftliche Arbeiten schreiben mit Word 2010}, 
    publisher = {Addison Wesley}, 
    year = {2011}, 
    author = {Nicol, N. and Albrecht, R.}, 
    address = {München}, 
    edition = {7} 
} 

@ARTICLE{shome, 
    author = {Scholz, S. and Menzl, S.}, 
    title = {Alle Wege führen nach Rom}, 
    journal = {Medizin Produkte Journal}, 
    year = {2011}, 
    volume = {18}, 
    pages = {243-254}, 
    subtitle = {ein Vergleich der regulatorischen Anforderungen und Medizinprodukte 
    in Europa und den USA}, 
    issue = {4} 
} 

@INBOOK{shu, 
    author = {Schulz, C.}, 
    title = {Corporate Finance für den Mittelstand}, 
    booktitle = {Praxishandbuch Firmenkundengeschäft}, 
    year = {2010}, 
    editor = {Hilse, J. and Netzel, W and Simmert, D.B.}, 
    booksubtitle = {Geschäftsfelder Risikomanagement Marketing}, 
    publisher = {Gabler}, 
    pages = {97-107}, 
    location = {Wiesbaden} 
}'; 

있어서, (Demo)

$pattern='/^@([^{]+)\{([^,]+),\s*$|^\s*([^\[email protected]=]+) = \{(.*?)}/ms'; 
if(preg_match_all($pattern,$bibtex,$out,PREG_SET_ORDER)){ 
    foreach($out as $line){ 
     if(isset($line[1])){ 
      if(!isset($line[3])){ // this is the starting line of a new set 
       if(isset($temp)){ 
        $result[]=$temp; // send $temp data to permanent storage 
       } 
       $temp=['type'=>$line[1],'unique_name'=>$line[2]]; // declare fresh new $temp 
      }else{ 
       $temp[$line[3]]=$line[4]; // continue to store the $temp data 
      } 
     } 
    } 
    $result[]=$temp; // store the final $temp data 
} 
var_export($result); 

출력 : 여기

array (
    0 => 
    array (
    'type' => 'BOOK', 
    'unique_name' => 'ko', 
    'title' => 'Wissenschaftlich schreiben leicht gemacht', 
    'publisher' => 'Haupt', 
    'year' => '2011', 
    'author' => 'Kornmeier, M.', 
    'number' => '3154', 
    'series' => 'UTB', 
    'address' => 'Bern', 
    'edition' => '4', 
    'subtitle' => 'für Bachelor, Master und Dissertation', 
), 
    1 => 
    array (
    'type' => 'BOOK', 
    'unique_name' => 'nial', 
    'title' => 'Wissenschaftliche Arbeiten schreiben mit Word 2010', 
    'publisher' => 'Addison Wesley', 
    'year' => '2011', 
    'author' => 'Nicol, N. and Albrecht, R.', 
    'address' => 'München', 
    'edition' => '7', 
), 
    2 => 
    array (
    'type' => 'ARTICLE', 
    'unique_name' => 'shome', 
    'author' => 'Scholz, S. and Menzl, S.', 
    'title' => 'Alle Wege führen nach Rom', 
    'journal' => 'Medizin Produkte Journal', 
    'year' => '2011', 
    'volume' => '18', 
    'pages' => '243-254', 
    'subtitle' => 'ein Vergleich der regulatorischen Anforderungen und Medizinprodukte 
    in Europa und den USA', 
    'issue' => '4', 
), 
    3 => 
    array (
    'type' => 'INBOOK', 
    'unique_name' => 'shu', 
    'author' => 'Schulz, C.', 
    'title' => 'Corporate Finance für den Mittelstand', 
    'booktitle' => 'Praxishandbuch Firmenkundengeschäft', 
    'year' => '2010', 
    'editor' => 'Hilse, J. and Netzel, W and Simmert, D.B.', 
    'booksubtitle' => 'Geschäftsfelder Risikomanagement Marketing', 
    'publisher' => 'Gabler', 
    'pages' => '97-107', 
    'location' => 'Wiesbaden', 
), 
) 

I로부터 새로운 샘플 입력 문자열을 추출하는 the site이다.