Tuesday, October 8, 2013

PHP, split summary text data from pubmed using preg_split and preg_match


I have summary text data from pubmed and I want to separate them into author, title, author, date and pubid etc. The PHP code:
<?php
//Journal list number and authorlist
$pmregex = '/^(\d+): (.+?)\.'

//article title
         . ' (.+?)\.'

// (.+?)  Journal name such as Oral Dis
// \. (\d\d\d\d)(.*?);  . 2013 Jul
// ;(.+?)     :19 (5)
//:(\w+)-?(\w*?) page number such as  440-451
// add \n? after considering 0+ line break
         . '(.+?)[\. \n?](\d\d\d\d)(.*?);(.+?):(\w+)-?(\w*?)[\.\;](.*)'
//PMID number
       . '[PMID: \n?](\d+)'
         . '/s';
$data="1: Saini R, Poh C. Photodynamic therapy: a review and its prospective role in the
management of oral potentially malignant disorders. Oral Dis. 2013
Jul;19(5):440-451. doi: 10.1111/odi.12003. PubMed PMID: 24079944.



2: Powell JJ, Apiratpracha W, Partovi N, Erb SR, Scudamore CH, Steinbrecher UP,
Buczkowski AK, Chung SW, Yoshida EM. Subcutaneous administration of hepatitis B
immune globulin in combination with lamivudine following orthotopic liver
transplantation: effective prophylaxis against recurrence. Clin Transplant. 2006
Jul-Aug;20(4):524-5. PubMed PMID: 16842532.
";
//[\r\n]: newline, \d+:  separator is digital numbers followed by : ,
//?=Separator is included in the output array
$choppeddata = preg_split('/([\r\n])(?=\d+: )/',$data);

foreach($choppeddata as $subdata)
{
print_r($subdata);
echo '<br /><br />';
preg_match($pmregex, trim($subdata), $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
}
?>

Running result:
1: Saini R, Poh C. Photodynamic therapy: a review and its prospective role in the management of oral potentially malignant disorders. Oral Dis. 2013 Jul;19(5):440-451. doi: 10.1111/odi.12003. PubMed PMID: 24079944.
Array
(
    [0] => 1: Saini R, Poh C. Photodynamic therapy: a review and its prospective role in the
management of oral potentially malignant disorders. Oral Dis. 2013
Jul;19(5):440-451. doi: 10.1111/odi.12003. PubMed PMID: 24079944
    [1] => 1
    [2] => Saini R, Poh C
    [3] => Photodynamic therapy: a review and its prospective role in the
management of oral potentially malignant disorders
    [4] =>  Oral Dis
    [5] => 2013
    [6] => 
Jul
    [7] => 19(5)
    [8] => 440
    [9] => 451
    [10] =>  doi: 10.1111/odi.12003. PubMed 
    [11] => 24079944
)
2: Powell JJ, Apiratpracha W, Partovi N, Erb SR, Scudamore CH, Steinbrecher UP, Buczkowski AK, Chung SW, Yoshida EM. Subcutaneous administration of hepatitis B immune globulin in combination with lamivudine following orthotopic liver transplantation: effective prophylaxis against recurrence. Clin Transplant. 2006 Jul-Aug;20(4):524-5. PubMed PMID: 16842532.
Array
(
    [0] => 2: Powell JJ, Apiratpracha W, Partovi N, Erb SR, Scudamore CH, Steinbrecher UP,
Buczkowski AK, Chung SW, Yoshida EM. Subcutaneous administration of hepatitis B
immune globulin in combination with lamivudine following orthotopic liver
transplantation: effective prophylaxis against recurrence. Clin Transplant. 2006 
Jul-Aug;20(4):524-5. PubMed PMID: 16842532
    [1] => 2
    [2] => Powell JJ, Apiratpracha W, Partovi N, Erb SR, Scudamore CH, Steinbrecher UP,
Buczkowski AK, Chung SW, Yoshida EM
    [3] => Subcutaneous administration of hepatitis B
immune globulin in combination with lamivudine following orthotopic liver
transplantation: effective prophylaxis against recurrence
    [4] =>  Clin Transplant
    [5] => 2006
    [6] =>  
Jul-Aug
    [7] => 20(4)
    [8] => 524
    [9] => 5
    [10] =>  PubMed 
    [11] => 16842532
) 
 

 
 
 

No comments:

Post a Comment