如何从给定的文本提取信息到数组


How extract information into array from given Text?

考虑一个形式为

的文本
@article{refregier2003weak,   
title={Weak gravitational lensing by large-scale structure},   
author={Refregier, Alexandre},  
journal={Annual Review of Astronomy and Astrophysics},  
volume={41}, 
pages={645},   
year={2003},   
publisher={Annual Reviews, Inc.} 
}

,其中"refregier2003weak"是文章的ID。标签、标题、作者、期刊的顺序……在某些情况下,某些标签可能会在某些文章中丢失。

如何提取这些标签的值和文章的ID在数组中使用PHP?

这里有一个快速的解决方案:

<?php
$input = '@article{refregier2003weak,   
title={Weak gravitational lensing by large-scale structure},   
author={Refregier, Alexandre},  
journal={Annual Review of Astronomy and Astrophysics},  
volume={41}, 
pages={645},   
year={2003},   
publisher={Annual Reviews, Inc.} 
}';
$pattern = '@('w+)='{(.*)'}@';
$articleIdPattern = '|@article'{(.*?),|';
preg_match_all($pattern, $input, $matches);
preg_match_all($articleIdPattern, $input, $articleMatches);
$result = [];
if (isset($matches[1]) && isset($matches[2])) {
    foreach ($matches[1] as $key => $value) {
        if (isset ($matches[2][$key])) {
            $result[$value] = $matches[2][$key];
        }
    }
}
if (isset($articleMatches[1][0])) {
    $result['article'] = $articleMatches[1][0];
}
var_dump($result);
http://ideone.com/K0WPVg

这可以通过简单的子字符串方法完成。例如:

$parsed = array();
foreach (explode("'n", $text) as $line) { // Split by line
    list ($key, $value) = explode('{', $line, 2); // Split by the first { ($key is text to the left, $val is text to the right)
    $value = substr($value, 0, strrpos($value, '}')); // Strip off everything after the last }
    switch ($key) {
        case '@article': $parsed['articleId'] = $value; break;
        case 'title={': $parsed['title'] = $value; break;
        case 'author={': // ...
    }
}
var_dump($parsed);

只要您一次只打算解析一个就可以。

记住为输入字符串更改$inputString的名称

<?php  
preg_match_all('/@article'{(.*?),/', $inputString, $id);
preg_match_all('/(.+)={(.*?)}/', $inputString, $meta);
$arrayResults = array("id" => $id[1][0]);
foreach($meta[1] as $key => $someMeta){
    $arrayResults[$someMeta] = $meta[2][$key];
}
var_dump($arrayResults);
?>

生产:

Array
(
    [id] => refregier2003weak
    [title] => Weak gravitational lensing by large-scale structure
    [author] => Refregier, Alexandre
    [journal] => Annual Review of Astronomy and Astrophysics
    [volume] => 41
    [pages] => 645
    [year] => 2003
    [publisher] => Annual Reviews, Inc.
)