【问题标题】:Find nearest "parameter 2" that appears before "parameter 1" when searching through a file搜索文件时查找出现在“参数 1”之前的最近的“参数 2”
【发布时间】:2013-09-02 09:57:52
【问题描述】:

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

当 ID 出现时,之前总会出现类似“找到一个 XML 文件”的语句,之后会出现“正在关闭 XML 文件”。这些代表我要复制的部分的“开始”和“结束”。

然后我想将此部分复制到另一个文本文件中。这将取代我在文件中查找 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 文件”?

【问题讨论】:

  • 您的 XML 文件是如何命名的?你的伪代码听起来不错,所以没有更具体的,我不确定..
  • 嗨,为了澄清我正在搜索文本文件,而不是 XML 文件。文本文件是解析 XML 的守护程序的输出。但是这行会出现像“Found an XML file OPA_4636367.xml”这三个字母的部分是有意义的,但是数字是任意的。

标签: php search text


【解决方案1】:

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

// 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. 检查以确保关闭 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 中至少有 2 个元素,则“弹出”$foundparts 的最后一个元素,因为它始终是包含文件名的元素
  6. 您现在拥有$filename 中的文件名,您可以随意使用

注意:这些函数区分大小写,因此如果您还想查找“找到一个 xml 文件”的实例(xml 为小写),您需要将字符串转换为全部小写$file$splitter$findme

【讨论】:

    【解决方案2】:
    <?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
        }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      相关资源
      最近更新 更多