2013-12-10 114 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. 如果有至少一個關鍵字匹配,打印。

資源: