使用PHP从包含相同单词的文本文件中读取数据

时间:2012-12-25 03:08:42

标签: php file

我有如下文字文件:

的text.txt

123  a  234 test    a1
123  a  234 test    a2
123  a  234 test    a3
1h3  b  234 test    a4
1d3  c  234 test    a5
12g  d  234 test    a6
v23  e  234 test    a7
n23  f  234 test    a8
1n3  g  234 test    a9
1k3  h  234 test    a10
1j3  i  234 test    a11
1r3  j  234 test12  a12
1s3  k  234 test34  a13
1w3  l  234 test56  a14

我想通过对重复多次的单词test进行排序来阅读它,所以我想得到如下结果:

test  a1
      a2
      a3
      a4
      a5

PHP代码:

 $PMTA_FILE = file_get_contents("text.txt");
            $lineFromText = explode("\n", $PMTA_FILE);          
            $chart = array();
            $row = 0;
            $domain = array();              
            natsort($lineFromText);
            $i = 0;
            foreach($lineFromText as $line){
                if($line !=""){
                    $data = explode(";",$line);
                    $domain = $data[3];
                    if (!array_key_exists($domain, $domains_seen)){
                        if($row < 5){
                            echo $domain;
                            /*In  $data[3],it has test 10 times that the same word so I just  want to display :
                            test  a1
                                  a2
                                  a3
                                  a4
                                  a5*/
                        }
                        $i++;
                    }
                    $domains_seen[$domain]==true;
                }
            }

请帮助我,谢谢。

1 个答案:

答案 0 :(得分:0)

我希望这就是你要找的东西。下次尝试搜索!

https://stackoverflow.com/a/6281852/1533203