从.txt文件中提取数据(PHP)

时间:2012-10-17 20:30:28

标签: php parsing

使用PHP,有没有更好的方法从文本文件中提取适当的信息,而不使用strpos和substr PHP函数?

我需要提取“课程xxx”,否和参考

示例:使用“课程1 ......”的主题进行记录的结果将是:

  • 课程1
  • 8415
  • 152

示例.txt文件:

Name: Dave
Age: 15
Subject: Course 1 (No: 8415, Ref: #152#)
Description:

Description 1



Name: John
Age: 28
Subject: Course 2 (No: 646544, Ref: #325#)
Description:

Description 1



Name: Steve
Age: 22
Subject: Course 3 (No: 545, Ref: #451#)
Description:

Description 1

编辑:注意到我不需要提取所有数据,但所有数据仍然存在于文件中。

4 个答案:

答案 0 :(得分:3)

if(preg_match_all('~'.
    'Name:\\s*(?<Name>.+?)\r?\n'. // Name
    'Age:\\s*(?<Age>[0-9]+)\r?\n'. // Age
    'Subject:\\s*(?<Subject>.+?)\\s*\\('. // Subject
        'No:\\s*(?<No>[0-9]+)\\s*,'. // No
        '\\s*Ref:\\s*#(?<Ref>[0-9]+)#'. // Ref
    '\\)\r?\n'. // /Subject
    'Description:\\s*(?<Description>.+?)\r?\n'. // Description
'~si', $AccountDump, $Matches)){
    $Names = $Matches['Name'];
    $Ages = $Matches['Age'];
    $Subjects = $Matches['Subject'];
    $Nos = $Matches['No'];
    $Refs = $Matches['Ref'];
    $Descriptions = $Matches['Description'];
    $Accounts = array();
    foreach($Names as $Key => $Name){
        $Accounts[$Key] = array_map('trim', array(
            'Name'              => $Name,
            'Age'               => $Ages[$Key],
            'Subject'           => $Subjects[$Key],
            'No'                => $Nos[$Key],
            'Ref'               => $Refs[$Key],
            'Description'       => $Descriptions[$Key],
        ));
    }
    // Got them!
    var_dump($Accounts);
}

在名为 $ AccountDump 的变量中加载文字。

玩得开心。测试你的样品,它的工作原理。 我已经拆分了RegExp,因此您可以根据需要进行跟踪。

希望它有效!

答案 1 :(得分:1)

你可能想要使用正则表达式。它会变得有点复杂,但不会像strpossubstr那样糟糕。

作为一个起点,这里是一个匹配name:value pairs的正则表达式 -

$matches = array();
preg_match_all('/^([^\s:]+):\s*(.+)$/m', $data, $matches);

print_r($matches);

编辑:我很好奇并完成了正则表达式,这里完全是 -

preg_match_all('/^([^\s:]+):\s*(.+?)(?:\s*\(([^\s:]+):\s*(.+),\s*([^\s:]+):\s*(.+)\))?$/m', $data, $matches);

答案 2 :(得分:1)

你可以拥有

$data = file_get_contents("log.txt");
$data = array_chunk(array_filter(array_map("trim",explode(chr(13).chr(10).chr(13), $data))),2);
$lists = array();

foreach ( $data as $value ) {
    $list = array();
    foreach ( explode("\n", implode("", $value)) as $item ) {
        list($key, $value) = explode(":", $item);
        $list[trim($key)] = trim($value);
    }
    $lists[] = $list;
}
var_dump($lists);

输出

array
  0 => 
    array
      'Name' => string 'Dave' (length=4)
      'Age' => string '15' (length=2)
      'Subject' => string 'Course 1 (No' (length=12)
      'Description' => string 'Description 1' (length=13)
  1 => 
    array
      'Name' => string 'John' (length=4)
      'Age' => string '28' (length=2)
      'Subject' => string 'Course 2 (No' (length=12)
      'Description' => string 'Description 1' (length=13)
  2 => 
    array
      'Name' => string 'Steve' (length=5)
      'Age' => string '22' (length=2)
      'Subject' => string 'Course 3 (No' (length=12)
      'Description' => string 'Description 1' (length=13)

答案 3 :(得分:0)

看看这两个PHP函数:

preg_replace

preg_match_all

相关问题