簡體   English   中英

如何解析從文本文件中獲取的數據?

[英]How to parse data that is being fetched from a text file?

我正在從需要匹配 substring 以獲取匹配行的文本文件中獲取數據。 有一次,我需要在分隔符“|”之后的行中獲取第三個 8 位值。 基本上,所有值都有不同的長度,並由分隔符“|”分隔。 除了第一個 substring (id) 具有固定長度並且具有固定的開始和結束 position。

文本文件數據示例:

    0123456|BHKAHHHHkk|12345678|JuiKKK121255
    9100450|HHkk|12348888|JuiKKK10000000021sdadad255
$file = 'file.txt';


// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$txt = explode("\n",$contents);

$counter = 0;
foreach($txt as $key => $line){
    $subbedString = substr($line,2,6);

   // $searchfor = '123456';
    //echo strpos($subbedString,$searchfor); 
    if(strpos($subbedString,$searchfor) === 0){
        $matches[$key] = $searchfor;
        $matchesLine[$key] = substr($line,2,50);
          echo  "<p>" . $matchesLine[$key] . "</p>";
          
                  $counter += 1;
                  if($counter==10) break;
         
    }

    
  1. 如果您需要用換行符分隔文件的內容,最好使用文件function
  2. 要通過分隔符將行分成長度未知的部分,請使用explode function。

代碼:

$file = 'file.txt';
$txt = file($file);

$counter = 0;
foreach ($txt as $key => $line) {
    $line = \trim($line);
    $substrings = explode('|', $line);
    
    if (\count($substrings) === 0) {
        continue;
    }

    $searchFor = '123456';
    if (substr($substrings[0], 1) === $searchFor) {
        if (!isset($substrings[2]) {
            continue;
        }

        $matches[$key] = $searchFor;

        $matchesLine[$key] = $line;
        echo  "<p>" . $substrings[2] . "</p>";

        if (++$counter === 10) {
            break;
        }
    }
}

我還注意到,在您的示例中,有 7 位 id,而您在談論 6 位(並且$searchfor變量不匹配任何內容)

利用

^(\d+)\|[^|]*\|(\d{8})\|

請參閱正則表達式證明

解釋

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \|                       '|'
--------------------------------------------------------------------------------
  [^|]*                    any character except: '|' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  \|                       '|'
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    \d{8}                    digits (0-9) (8 times)
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  \|                       '|'

示例代碼

<?php

$re = '/^(\d+)\|[^|]*\|(\d{8})\|/m';
$str = '0123456|BHKAHHHHkk|12345678|JuiKKK121255
9100450|HHkk|12348888|JuiKKK10000000021sdadad255';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);

樣品 output

array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(28) "0123456|BHKAHHHHkk|12345678|"
    [1]=>
    string(7) "0123456"
    [2]=>
    string(8) "12345678"
  }
  [1]=>
  array(3) {
    [0]=>
    string(22) "9100450|HHkk|12348888|"
    [1]=>
    string(7) "9100450"
    [2]=>
    string(8) "12348888"
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM