我将如何比较两个文本文件用PHP比赛 [英] How would I compare two text files for matches with PHP

查看:94
本文介绍了我将如何比较两个文本文件用PHP比赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $域=文件(../../ domains.txt');
$关键字=文件(../../ keywords.txt');

$域将在格式为:

  3kool4u.com 9 /2013分之2912:00:00 AM,AUC
3liftdr.com 9 /2013分之2912:00:00 AM,AUC
3lionmedia.com 9 /2013分之2912:00:00 AM,AUC
3mdprod.com 9 /2013分之2912:00:00 AM,AUC
3mdproductions.com 9 /2013分之2912:00:00 AM,AUC

关键词将在格式为:

 关键字1
1关键字
KEYWORD3

我想我真的想从一个文件做关键字的数组,并搜索匹配domains.txt的每一行。不知道从哪里开始因为我在preg_match,preg_match_all的差异感到困惑,并strpos或多或少时使用一个比其他。

提前感谢您的帮助。


解决方案

  //空数组认为有一个匹配的域上的每一行
$匹配=阵列();//对域文件中的每一行
的foreach($域为$域){    //为每个关键字
    的foreach($关键字$关键字){          //如果域名行包含的任何位置上的关键字不管情况
          如果(preg_match(/ $关键字/我,$域)){
                    //域行添加到阵列匹配
            $匹配[] = $域;
          }
     }
}

现在你有$匹配阵列匹配关键字的域名文件的所有行

的注意,与preVIOUS接近两类整文件被加载到内存中,并根据文件的大小,您可以运行内存不足或操作系统将开始使用SWAP这是比RAM

慢得多

这是另一个更有效的方法,可以加载一条线,如果该文件的时间。

 < PHP//允许行结束的自动检测
的ini_set('auto_detect_line_endings',真);//阵列将保存匹配的行
$匹配=阵列();//开幕读模式下这两个文件
$ domains_handle =的fopen('../../ domains.txt,R);
$ keywords_handle =的fopen('../../ keywords.txt,R);    //迭代域一行在当时
    而(($ domains_line =与fgets($ domains_handle))!== FALSE){        //有关域文件中的每一行,迭代kwywords当时文件中的行
        而(($ keywords_line =与fgets($ keywords_handle))!== FALSE){              //删除从一开始就任何空格或新行或字符串的结尾
              $ trimmed_keyword =修剪(keywords_line $);              //检查域行包含上的任何位置关键字
              使用区分大小写的比较//
              如果(preg_match(/ $ trimmed_keyword /我,修剪($ domains_line))){
                    //域行添加到阵列匹配
                $匹配[] = $ domains_line;
              }
        }
        //指针设置为关键字文件的开头
        倒带($ keywords_handle);
    }//释放资源
FCLOSE($ domains_handle);
FCLOSE($ keywords_handle);后续代码var_dump($比赛);

$domains = file('../../domains.txt');
$keywords = file('../../keywords.txt');

$domains will be in format of:

3kool4u.com,9/29/2013 12:00:00 AM,AUC
3liftdr.com,9/29/2013 12:00:00 AM,AUC
3lionmedia.com,9/29/2013 12:00:00 AM,AUC
3mdprod.com,9/29/2013 12:00:00 AM,AUC
3mdproductions.com,9/29/2013 12:00:00 AM,AUC

keywords will be in format of:

keyword1
keyword2
keyword3

I guess I would really like to do an array for keywords from a file and search each line of domains.txt for matches. Not sure where to start as I'm confused at the difference of preg_match, preg_match_all, and strpos and more or less when to use one over the other.

Thanks ahead for the help.

解决方案

//EMPTY array to hold each line on domains that has a match
$matches = array();

//for each line on the domains file
foreach($domains as $domain){

    //for each keyword
    foreach($keywords as $keyword){

          //if the domain line contains the keyword on any position no matter the case
          if(preg_match("/$keyword/i", $domain)) {
                    //Add the domain line to the matches array
            $matches[] = $domain;
          }     
     }   
}

Now you have the $matches array with all the lines of the domain file that match the keywords

NOTE THAT WITH THE PREVIOUS APPROACH THE TWO ENTIRE FILES ARE LOADED INTO MEMORY AND DEPENDING ON THE FILE SIZES YOU CAN RUN OUT OF MEMORY OR THE OS WILL START USING THE SWAP WHICH IS MUCH SLOWER THAN RAM

THIS IS ANOTHER AND MORE EFFICIENT APPROACH THAT WILL LOAD ONE LINE IF THE FILE AT THE TIME.

<?php

// Allow automatic detection of line endings
ini_set('auto_detect_line_endings',true);

//Array that will hold the lines that match
$matches = array();

//Opening the two files on read mode
$domains_handle = fopen('../../domains.txt', "r");
$keywords_handle = fopen('../../keywords.txt', "r");

    //Iterate the domains one line at the time
    while (($domains_line = fgets($domains_handle)) !== false) {

        //For each line on the domains file, iterate the kwywords file a line at the time
        while (($keywords_line = fgets($keywords_handle)) !== false) {

              //remove any whitespace or new line from the beginning or the end of string
              $trimmed_keyword = trim($keywords_line);

              //Check if the domain line contains the keyword on any position
              // using case insensitive comparison
              if(preg_match("/$trimmed_keyword/i", trim($domains_line))) {
                    //Add the domain line to the matches array
                $matches[] = $domains_line;
              } 
        }
        //Set the pointer to the beginning of the keywords file
        rewind($keywords_handle);
    }

//Release the resources
fclose($domains_handle);
fclose($keywords_handle);

var_dump($matches);

这篇关于我将如何比较两个文本文件用PHP比赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆