1

问题:

我想使用 PHP 从文本文件中提取特定信息,并为这些信息创建两个数组。

示例文本文件:

在此处输入图像描述

期望的输出:

  1. /Perspective 的关键字应该在第一个数组中:skincover、智能和腿
  2. 列关键字应该在第二个数组中:腿、皮肤覆盖、体重、智力、速度

应忽略以 / 开头的列关键字

到目前为止的代码:

<?php   
    $file = file('Creatures_rich.txt');

    foreach($file as $line_num => $line)
    {
        if (eregi("^/Perspective", $line))
             $perspective = explode(' ', trim(str_replace('/Perspective:', '', $line)));
    }

    echo "<xmp>".print_r($perspective, true)."</xmp>";
?>

到目前为止的输出:

Array
(
    [0] => skincover
    [1] => intelligence
    [2] => legs
)

我将如何开始处理第二个数组?欢迎任何想法,任何代码示例都受到高度赞赏。

4

1 回答 1

1

解决方案

假设只有一行被标记,并且开头/Perspective:的第一行是列标题行......\t

过度评论(为了清楚起见)

$perspectives = array();              //Initialise perspectives array
$columns      = array();              //Initialise column names array
$text_file    = fopen('./file', 'r'); //Open file to handle

while($line = fgets($text_file)){                        //Read file line by line
    if(strpos($line, '/Perspective:') === 0){            //Check if '/Perspective:' is at start of string
        $perspectives = explode(' ', substr($line, 14)); // Remove first 14 characters: /Perspective: 
        continue;
    }
    else if(strpos($line, "\t") === 0){ //Check if first char in line is \t
        $columns = explode("\t", 
                            preg_replace("#\t/.+#", '', substr($line, 1)) //Remove commented column names and first \t
                          );
        break; // Break while loop after column names row
    }
 }

未注释的代码

$perspectives = array();
$columns      = array();
$text_file    = fopen('./file', 'r');
while($line = fgets($text_file)){
    if(strpos($line, '/Perspective:') === 0){
        $perspectives = explode(' ', substr($line, 14));
        continue;
    }
    elseif(strpos($line, "\t") === 0){
        $columns = explode("\t", 
                            preg_replace("#\t/.+#", '', substr($line, 1))
                          );
        break;
    }
 }

输入文件

/Purpose: Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nam, suscipit incidunt doloribus voluptatum dicta maxime accusantium animi eum vero eaque odit quae non quaerat possimus enim ad numquam consequuntur beatae.
/Origin: Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima, animi minus perspiciatis laudantium? Nostrum, aspernatur, sequi ratione assumenda fuga similique architecto deleniti sint recusandae voluptatibus numquam obcaecati ducimus eaque nisi.
/Rawdata: Unknown
/Perspective: skincover intelligence legs
/Lorem ipsum dolor sit amet, consectetur adipisicing elit. Porro, libero, accusamus laboriosam modi voluptatem facere quod unde atque perferendis laborum nisi omnis nihil cum minima quaerat. Quia, quaerat ipsa molestiae.
    legs    skincover   weight  intelligence    speed   /something  /else
dog 1   1   1   1   1   1   1
pig 1   1   1   1   1   1   1
human   1   1   1   1   1   1   1

边注

出于好奇,我用您的代码(代码 A)与我的代码(代码 B )对比,看看哪个表现更好。

结果

执行时间:

Code A: 0.000108
Code B: 0.000044

代码 B2.4545454545速度要快几倍,并且可以执行整个操作perspecitves,并且column names

如果没有过多地分析这两个代码,我建议造成差异的主要原因是我们处理文件的方式。

注意

我确实进行了多次比较,差异大致从2.2x2.7x

此外,时间都非常小,所以这不是什么大不了的事……

于 2013-10-19T11:03:22.653 回答