Search code examples
phpregexparsingstring-parsing

Parsing a nested sentence and related with them PHP


I am doing a parsing operation on some data. I am taking this line from database. In this line : Between [ ] this things are the author names. There can be author more than one. After that [ ], to the end of the first , there is the university of the authors.

Example Line

[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.

For example for this line, the expected output should be:

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

Here is what i done right now :

enter image description here

My Code

    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

Any help is appriciated.


Solution

  • Ok, this works:

    $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 />";
        }   
    }
    

    Output:

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

    Working code example:

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