【问题标题】:How to modify this php function to check all lines before result execution?如何修改此 php 函数以在结果执行之前检查所有行?
【发布时间】:2020-03-28 18:25:58
【问题描述】:

我已经更正了一个代码,它应该只回显来自 file.html 的所有包含单词“TrxID”的“行”。但它返回其中包含“TrxID”的“段落”的第一个,因此有超过 3 行包含“TrxID”。

文件.html

    <html lang="en"> <body> <p> recieved $500<br /> to 862444947 successful<br /> Fee $ 9<br />25<br /> Balance Tk $1000<br />78<br /> TrxID 6DU2ATVQCO at 30/04/2019 19:25<br />download the app </p>

  <p> recieved $500<br /> to 862444947 successful<br /> Fee $ 9<br />25<br /> Balance Tk $1000<br />78<br /> Tr 6DU2ATVQCO at 30/04/2019 19:25<br />download the app </p>

  <p> recieved $500<br /> to 862444947 successful<br /> Fee $ 9<br />25<br /> Balance Tk $1000<br />78<br /> TrxID 6DU2ATVQgfCO at 30/04/2019 19:25<br />download the app </p>

  <p> recieved $500<br /> to 862444947 successful<br /> Fee $ 9<br />25<br /> Balance Tk $1000<br />78<br /> 6DU2ATVQCO at 30/04/2019 19:25<br />download the app </p> </body> </html>        

函数.php

   <?php  function checkFile( $file, $keyword ) {

// open file for reading
$handle = @fopen( $file, 'r' );

// check to make sure handle is valid
if( $handle ) {

    // traverse file line by line
    while( ($line = fgets($handle)) !== false ) {

        // search for specific keyword no matter what case is used i.e. trxid or TrxID
        if( stripos($line, $keyword) === false ) {
            // string not found, continue with next iteration
            continue;
        } else {

            // keyword was found

            // close file
            fclose($handle);

            // return line
            return $line;
        }
    }
} } $result = checkFile( 'file.html', 'TrxID' );  echo $result; ?>      

结果

recieved $500 to 862444947 successful Fee $ 9 25 Balance Tk $1000 78 TrxID 6DU2ATVQCO at 30/04/2019 19:25 download the app

我想要:

TrxID 6DU2ATVQCO at 30/04/2019 19:25

现在我想确认一下,

  1. 它应该只返回包含“TRXID”的行,而不是段落,如果我将 br/ 替换为“段落标签”结果保持不变
  2. 它应该显示所有行,而不是单个结果。在这个函数上它只返回第一个而不是全部。

希望您的帮助。在此先感谢。

【问题讨论】:

    标签: php function line execution


    【解决方案1】:

    我稍微修改了你的代码:

    function checkFile($file, $keyword)
    {
        $handle = @fopen($file, 'r');
        if ($handle) {
            while (($line = fgets($handle)) !== false) {
                if (stripos($line, $keyword) === false) {
                    continue;
                } else {
                    $exploded = explode('<br />', $line);
                    foreach ($exploded as $e) {
                        if (stripos($e, $keyword) !== false) {
                            $lines[] = substr($e, 0, strpos($e, "at"));
                        }
                    }
                }
            }
        }
        return $lines;
        fclose($handle);
    }
    $result = checkFile('file.html', 'TrxID');
    print_r($result); // return array of lines
    

    或者如果你想回应它:

    echo $output = implode('&lt;br /&gt;', $result);

    如果你想要 trxid 之后的字符串:

    function checkFile($file, $keyword)
    {
        $fileContents = file_get_contents($file);
        $result = preg_match_all('/(?<=\b'.$keyword.'\s)(?:[\w-]+)/is', $fileContents, $matches);
        return preg_filter('/^/', $keyword.' ', $matches[0]);
    }
    
    $result = checkFile('file.html', 'TrxID');
    print_r($result); // return array of lines
    

    【讨论】:

    • Mike ,它有效,但它是否区分大小写?因为如果我将 TrxID 之一更改为 trxid,则 trxid 的结果不显示!!?
    • 我编辑了代码,你可以试试。只需将 strpos 更改为 stripos。
    • 迈克,如果我只想在 2019 年 4 月 30 日 19:25 获得“TrxID 6DU2ATVQCO”而不是“TrxID 6DU2ATVQCO”,您有什么建议?
    • 实际上,它已经达到了我预期的一半,现在它正在返回:“收到 500 美元到 862444947 成功v 费用 92.5 美元余额 Tk 1000 美元 78 TrxID 6DU2ATVQCO”不是:“TrxID 6DU2ATVQCO”,任何建议如何做它? ;-)
    • 你能发布你的 file.html 吗?对我来说,它的工作和返回:TrxID 6DU2ATVQCO 编辑:我在我的答案中添加了其他代码,如果你想要 trxid 之后的字符串,但两者都在工作
    【解决方案2】:

    另一种方法是您可以将整个文件内容传递到 preg_match 中,您将在 $matches 中返回一个匹配项的数组。

    $result = preg_match('/TrxID .+ at [\d]{2}\/[\d]{2}\/[\d]{4} [\d]{2}:[\d]{2}/i', $fileContents, $matches)
    
    print_r($matches);
    

    【讨论】:

    • 它漂亮的colonelclick,我尝试使用preg_match 但失败了,那是我的错误。但是这些代码是必要的,因为我没有修复它就完成了我的项目!如果我要将其更改为您的,那么我必须进行更多编辑,这是我不想要的,但我会记住您的建议。 :-)
    猜你喜欢
    • 2011-09-19
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    相关资源
    最近更新 更多