2013-12-10 116 views
0
后继续搜索文件

基本上我有这样的事情如何代码运行PHP

Hand #1 

First row always has the same info, 
if the text matches what im looking for ill find 
the keyword in the first line. Bunch of text, 
bunch more text bla bla bla 

Hand #2 

这是我的代码打印出手工#1和手#2

$searchfor = 'myKeyword'; 
$file = file_get_contents($filename); 

// find the location of the keyword, this keyword indicates that i want to grab this group 
// of text, since each group of text starts off with Hand #x and ends immediately before the next Hand #x i search for the keyword to identify this is a valid group of text 
$pos_keyword = strpos($file, $searchfor); 

// there might be a more elegant way but the Hand # value i need will always be within 60-70 characters before the keyword 
$rollback = $pos_keyword-100; 

// this is the start position of the text i want to grab 
$start = strpos($file, "Hand #", $rollback); 
// we search from the after the keyword and assign to $end 
$end = strpos($file, "Hand #", $pos_keyword); 


// print out the string between the start and end Hand# keywords 
echo "string: " . substr($file,$start,($end-$start)) . "<br />"; 
echo "<br /><br /><br />"; 
之间的所有文字

现在文档有数百个这些值,我想重复搜索直到文档结束。我尝试了谷歌搜索,但人们提到使用!eof($文件)可能会导致循环,我无法让它工作,任何想法,我会用什么函数或循环遍历代码,直到文档结束。

我猜我循环,并在最后设置$结束为新的$ pos_keyword但我不知道什么样的循环是最好的使用,任何想法?

+0

你可以在术语'\ nHand#'上'爆炸'它。 –

回答

2

搜索一个关键字,然后回溯可能不是你以后,因此这将是我的建议;先分割这些部分,然后根据它们是否包含关键字对它们进行过滤:

$text = <<<EOS 
Hand #1 

First row always has the same info, 
if the text matches what im looking for ill find 
the keyword in the first line. Bunch of text, 
bunch more text bla bla bla 

Hand #2 

Lala alala 
EOS; 

$keyword = 'keyword'; 
$block_re = '/(^Hand #)(\d+)(.*?)(?=\1|\Z)/ms'; 

if (preg_match_all($block_re, $text, $matches, PREG_SET_ORDER)) { 
    print_r(array_filter($matches, function($match) use ($keyword) { 
     return strpos($match[3], $keyword); 
    })); 
} 

这将仅返回第一个段;第二个不包含“关键字”。

0

它不是很经常我会这么说,但正则表达式可能是一个可行的选择,在这里...请看下面的正则表达式:

/Hand #1(.*?)Hand #2/s 

/s修饰符允许.以匹配新线

所以你这样做:

$file = file_get_contents($filename); 
$matches = array(); 

preg_match('/Hand #1(.*?)Hand #2/s', $file, $matches); 

print_r($matches); 

现在$matches包含两个键(如果找到你想要的东西) - 的0指数有整个字符串,1索引具有匹配的文本。 See this example here.

整理,并返回你的匹配文本,这样做:

unset($matches[0]); 
$return_text = trim($matches[1]); 

循环

现在,我猜想Hand #1 -> Hand #2是在你的文件每个块的不同。如果是这样的话,你知道他们是你循环之前的东西,你可以做这样的事情:

$delimiters = array('Hand', 'Dog', 'Cat', 'Person', 'Etc'); 
$returns = array(); 

foreach($delimiters as $d) { 
    $matches = array(); 
    preg_match('/' . $d . ' #1(.*?)' . $d . ' #2/s', $file, $matches); 
    if(!empty($matches[1])) 
     $returns[] = trim($matches[1]); // add to output array 
} 

在这个月底,你的$returns数组将包含所有这些分隔符之间的所有匹配块。

如果你的分隔符是所有Hand #1Hand #2,你需要使用preg_match_all,这将返回一个包含所有匹配块的数组,你会不会需要一个循环(和零指数,你会取消设置)。

文档

实例

0

首先,让我尝试重申你的问题我的理解是:

你必须具有以下格式的文件:

Hand #1 
Some text with keywords like apple 
Some more text 
... 
Last line of Block 
Hand #2 
Oranges are good too 
This one only has 2 lines 
Hand #3 

等等。

你想要的代码将遍历输入文本的所有行并输出关键字匹配的完整代码块。

$keywords = array('apple', 'orange'); 

$handle = @fopen($filename, "r"); 

if ($handle) { 
    $block = ""; //redundant, really 

    //read through the file. When we hit 'Hand #', start filling up $block 
    while (($line = fgets($handle, 4096)) !== false) { 
     if(strpos($buffer, 'Hand #') === 0){ 
      foreach($keywords as $keyword){ 
       if(stripos($block, $keyword) !== false){ 
        print "string: {$block}<br />"; 
        break; //only need to match one keyword to print the block 
       } 
      } 

      print "<br /><br /><br />"; 
      $block = ""; //this is the beginning of a block; 
     } 

     $block .= $line; 
    } 
    if (!feof($handle)) { 
     echo "Error: unexpected fgets() fail\n"; 
    } 

    //check the final block 
    foreach($keywords as $keyword){ 
     if(stripos($block, $keyword) !== false){ 
      print "string: {$block}<br />"; 
      break; //only need to match one keyword to print the block 
     } 
    } 

    fclose($handle); 
} 

简而言之:

  1. 循环通过在每次一行。
  2. 如果某行以“手#”开始,我们应该有文字的完整块内置
  3. 查看我们的文本块对我们的关键字列表
  4. 如果有至少一个关键字匹配,打印。

资源: