2014-02-25 4 views
0

일부 데이터에 대해 구문 분석 연산을 수행하고 있습니다. 나는이 줄을 데이터베이스에서 가져 가고있다. 이 행의 내용 : [ ] 사이에이 것들이 저자 이름입니다. 하나 이상의 저자가있을 수 있습니다. 그 후 [ ], 첫 번째 ,의 끝에는 저자의 대학이 있습니다.중첩 된 문장의 구문 분석 및 관련 PHP PHP

라인이 라인 예컨대

[Joseph, Susan; Forsythe, Stephen J.] Nottingham Trent Univ, Pathogen Res Ctr, Sch Sci & Technol, Nottingham NG11 8NS, England. %#[Cetinkaya, Esin; Ayhan, Kamuran] Oxford Univ, Fac Engn, Dept Food Engn,06110 8NS, England.

, 예상 출력 같아야

Susan Joseph : Nottingham Trent Univ
Stephen J. Forsythe : Nottingham Trent Univ
Esin Cetinkaya : Oxford Univ
Kamuran Ayhan : Oxford Univ

여기

내가 지금 무슨 짓입니다 :

enter image description here

내 코드 어떤 도움이 appriciated됩니다

This code is taking the authors from that line but i cannot related them with their universities

while($row = mysqli_fetch_array($result)) 
     { 
     echo "<br>"; 
     echo $row['Correspounding_Author'] ; 
     echo "<br>"; 
     $pattern = '~(?<=\[|\G;)([^,]+),([^;\]]+)~'; 
     if (preg_match_all($pattern, $row['Correspounding_Author'], $matches, PREG_SET_ORDER)) { 
     print_r(array_map(function($match) { 
      return sprintf('%s %s', ltrim($match[2]), ltrim($match[1])); 
     }, $matches)); 

     } 
} 
.

답변

2

좋아,이 작품 :

$teststring = '[Joseph, Susan; Forsythe, Stephen J.] Nottingham Trent Univ, Pathogen Res Ctr, Sch Sci & Technol, Nottingham NG11 8NS, England. %#[Cetinkaya, Esin; Ayhan, Kamuran] Oxford Univ, Fac Engn, Dept Food Engn,06110 8NS, England.'; 

preg_match_all('/\[([^\]]+)]([^,]+)/', $teststring, $matches, PREG_SET_ORDER); 

foreach ($matches as $match) { 
    $authors = explode(";", $match[1]); 
    foreach ($authors as $author) { 
     echo preg_replace("/([^,]+),\s?(.*)/", "$2 $1", $author)." : ".$match[2]."<br />"; 
    } 
} 

출력 :

Susan Joseph : Nottingham Trent Univ 
Stephen J. Forsythe : Nottingham Trent Univ 
Esin Cetinkaya : Oxford Univ 
Kamuran Ayhan : Oxford Univ 

작동 코드 예제 :

http://sandbox.onlinephpfunctions.com/code/1f9b68427fa71f74c3e6b775a3dbc3202f2c0d4a