PHP 如何搜索文件中的特定变量并显示连接到它的所有行 - 请参阅详细信息



我需要做的是在文本中找到特定的唯一编号,这在文档中可能会多次出现。 我需要显示连接到它的所有行以及所有出现的情况。如果您检查文件示例,您可以看到常见的分隔符是 ===。所以我正在考虑使用一些正则表达式来检查搜索的数字并显示两个 === 之间的所有行。 并在preg_match的帮助下运行搜索文件。目前我不知道如何写正则表达式,你能帮我吗?

文件简单:

=======================================================
= Elapsed: yxz ms
= In msg: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><BLRequest xmlns="http://www.wwww.ww/wwww/">  *** <find_me>12345678901</find_me> *** </BLRequest></soapenv:Body></soapenv:Envelope>
= Out msg: <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/" envelope:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> *** </Body></Envelope>
=======================================================
=======================================================
= Elapsed: yxz ms
= In msg: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><BLRequest xmlns="http://www.wwww.ww/wwww/">  *** <find_me>6545678901</find_me> *** </BLRequest></soapenv:Body></soapenv:Envelope>
= Out msg: <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/" envelope:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> *** </Body></Envelope>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http " > *** </SOAP-ENV:Envelope>
=======================================================
=======================================================
= Elapsed: yxz ms
= In msg: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><BLRequest xmlns="http://www.wwww.ww/wwww/">  *** <find_me>12345678901</find_me> *** </BLRequest></soapenv:Body></soapenv:Envelope>
= Out msg: <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/" envelope:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> *** </Body></Envelope>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http " > *** </SOAP-ENV:Envelope>
=======================================================

我还写了一个代码,应该在哪里插入这个正则表达式,可以还是有更好的方法可以做到这一点?

<?php
$file ='myFile.log';
$search = '12345678901'; //find_me
$pattern = '/(?<========================================================).*?(?========================================================)/s';

$file = fopen($file, "r") or die("Cannot open file!n"); 
while ($line = fgets($file, 1024)) { 
    if (preg_match($pattern, $line)) { 
        echo "Results:</br> " . $line; 
    } else { 
        echo "No match: " . $line; 
    } 
} 
fclose($file); 
?>

请注意:文件最大可达 200Mb。当前的RE不正确,这是我的尝试之一另外,是否可以将结果显示为 xml ?由于文件中有 XML 行?

处理大文件的另一种解决方案:

$filename = 'file.txt';
$search   = '12345678901'; //find_me
$pattern  = "/(?<=====)(?:(?!====).)+($search)(?:(?!====).)+(?=====)/s";
$handle  = fopen($filename , "r") or die("Cannot open $filename!n"); 
$out = array();
$start = 0;
$found = 0;
while($line = fgets($handle)) {
    if (preg_match('/^==========/', $line)) {
        if ($start) {
            if ($found) {
                foreach($out as $ll) echo $ll,"n";
            }
            $found = 0;
            $start = 0;
            $out = [];
        } else {
            $start = 1;
            $out[] = $line;
        }
    } else {
        if ($start) {
            $out[] = $line;
            if (preg_match("/$search/", $line)) $found = 1;
        }
    }
}
fclose($handle); 

输出:

=======================================================
= Elapsed: yxz ms
= In msg: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><BLRequest xmlns="http://www.wwww.ww/wwww/">  *** <find_me>12345678901</find_me> *** </BLRequest></soapenv:Body></soapenv:Envelope>
= Out msg: <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/" envelope:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> *** </Body></Envelope>
=======================================================
= Elapsed: yxz ms
= In msg: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><BLRequest xmlns="http://www.wwww.ww/wwww/">  *** <find_me>12345678901</find_me> *** </BLRequest></soapenv:Body></soapenv:Envelope>
= Out msg: <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/" envelope:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> *** </Body></Envelope>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http " > *** </SOAP-ENV:Envelope>
<</div> div class="one_answers">

我会阅读整个文件,然后针对模式进行测试。模式必须包含搜索:

$filename = 'file.txt';
$search   = '12345678901'; //find_me
$pattern  = "/(?<=====)(?:(?!====).)+($search)(?:(?!====).)+(?=====)/s";
$handle  = fopen($filename , "r") or die("Cannot open $filename!n"); 
$content = fread($handle, filesize($filename));
if (preg_match_all($pattern, $content, $matches)) { 
    print_r($matches);
} else { 
    echo "No match: n"; 
} 
fclose($handle); 

输出:

Array
(
    [0] => Array
        (
            [0] => ===
= Elapsed: yxz ms
= In msg: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><BLRequest xmlns="http://www.wwww.ww/wwww/">  *** <find_me>12345678901</find_me> *** </BLRequest></soapenv:Body></soapenv:Envelope>
= Out msg: <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/" envelope:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> *** </Body></Envelope>
            [1] => ===
= Elapsed: yxz ms
= In msg: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><BLRequest xmlns="http://www.wwww.ww/wwww/">  *** <find_me>12345678901</find_me> *** </BLRequest></soapenv:Body></soapenv:Envelope>
= Out msg: <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/" envelope:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> *** </Body></Envelope>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http " > *** </SOAP-ENV:Envelope>
        )
    [1] => Array
        (
            [0] => 12345678901
            [1] => 12345678901
        )
)

最新更新