如何修改此php函数以在结果执行前检查所有行?

问题描述 投票:-1回答:1

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

File.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>        

Function.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个回答
0
投票
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 (strpos($e, $keyword) !== false) {
                        $lines[] = $e;
                    }
                }
            }
        }
    }
    return $lines;
    fclose($handle);
}
$result = checkFile('file.html', 'TrxID');
print_r($result); // return array of lines

或者如果您想回显它:

echo $output = implode('<br />', $result);

© www.soinside.com 2019 - 2024. All rights reserved.