【问题标题】:Extract specific information from text file using PHP使用 PHP 从文本文件中提取特定信息
【发布时间】:2023-03-28 10:50:02
【问题描述】:

问题:

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

示例文本文件:

期望的输出:

  1. /Perspective 的关键字应该在第一个数组中:skincover、intelligence 和legs
  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
)

我将如何着手处理第二个数组?欢迎任何想法,任何代码示例都非常感谢。

【问题讨论】:

  • 您的文本数据格式是什么(excel、带分隔符的txt)?
  • 这是一个带有制表符分隔符的文本文件 (.txt)。屏幕截图是通过在 Excel 中打开文本文件制作的。
  • Desired 输出非常好,但 Required 代码不是。
  • 不确定我是否清楚。但我不是在寻找完整的解决方案,而是在寻找代码示例和指导以在此过程中学习。

标签: php arrays text extract


【解决方案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 快了几倍,并且可以完成整个操作 perspecitvescolumn names

如果不分析这两个代码太多,我认为造成差异的主要原因是我们处理文件的方式。

注意

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    相关资源
    最近更新 更多