查找最近的“;参数2”;出现在“;参数1”;当搜索文件时


Find nearest "parameter 2" that appears before "parameter 1" when searching through a file

我想使用PHP在txt文件目录中搜索可能出现在多个实例中的特定ID。

当ID出现时,总会有一条语句,如"Found an XML file"出现在之前,以及"Closing XML file"之后。这些语句表示我要复制的部分的"开始"answers"结束"。

然后我想把这个部分复制到另一个文本文件中。这将取代我在文件中查找ID,然后手动复制出相关部分的过程。

在伪代码中,我的想法是;

while(parsing text file)
  {
  if (current line == search_ID)
    {
    loop for "Found an XML file"
    start copying
    loop for "Closing XML file"
    output string to txt file
    }
  }

因此,我的问题是如何从搜索ID"向上"循环,直到找到最近的"找到一个XML文件"

您想要做的是将整个文件内容作为一个字符串读取,然后根据您在其中找到的内容将其拆分。如下所示:

// Read the contents of the file into $file as a string
$mainfilename = "/path/to/file.txt";
$handle = fopen($mainfilename, "r");
$file = fread($handle, filesize($mainfilename));
fclose($handle);
/* $file contains your file contents
 * $findme contains "Found an XML file"
 * $splitter contains "Closing XML file"
 */
// We only do anything if the string "Closing XML file" is inside the file
// in a place other than at the beginning of the file
if (strpos($file, $splitter) > 0) {
    // Break up $file into pieces by splitting it along "Closing XML file"
    $parts = explode($splitter, $file);
    // Traverse the newly-formed pieces
    foreach ($parts as $part) {
        // If we have "Found an XML file" contained in this piece of the file
        if (strpos($part, $findme) !== false) {
            // Split up our smaller string around "Found an XML file"
            $foundparts = explode($findme, $part);
            // The last piece will always contain the filename,
            // but only if there are two or more pieces
            // i.e. something between the strings
            if (count($foundparts) > 1) $filename = array_pop($foundparts);
            /* Do whatever you want with $filename */ 
        }
    }
}

这将做什么,假设$file == "Closing XML file gibberish goes here Found an XML file garbage Found an XML file filename.xls Closing XML file more gibberish":

  1. 检查以确保Closing XML文件存在于$file中的某个位置,而不是在开始处,即接近结束处
  2. $file拆分为多个部分:$parts = ['', ' gibberish goes here Found an XML file garbage Found an XML file filename.xls ', ' more gibberish']
  3. 遍历$parts查找"找到一个XML文件"的实例-$parts[1]有它
  4. $parts[1]拆分为多个部分:$foundparts = [' gibberish goes here',' garbage ', ' filename.xls ']
  5. 如果$foundparts中至少有两个元素,则"弹出"$foundparts的最后一个元素,因为它将始终是包含文件名的元素
  6. 您现在有了$filename中的文件名,可以随意处理

注意:这些函数区分大小写,因此,如果您还想查找"Found an xml file"(xml为小写)的实例,则需要对所有$file$splitter$findme 进行一些字符串转换,使其全部为小写。

<?php
// Ex: OPA_4636367.xml
foreach(glob("*.txt") as $file) {
    $file_designation = explode('_', $file);
    if ($file_designation[0] == 'OPA') {
        // XML found
        // Do file_get_contents($file) or whatver
    }
}
?>