解析一个嵌套的句子并与它们相关PHP

时间:2014-02-25 23:30:43

标签: php regex parsing string-parsing

我正在对某些数据进行解析操作。我从数据库中取这条线。 在这一行:  在[ ]之间这些东西是作者的名字。可以有不止一个作者。在[ ]之后,到第一个,的末尾,有作者大学。

示例行

[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

我的代码

    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));

      }
}

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

任何帮助都是适当的。

1 个答案:

答案 0 :(得分: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