【发布时间】: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
现在我想确认一下,
- 它应该只返回包含“TRXID”的行,而不是段落,如果我将 br/ 替换为“段落标签”结果保持不变
- 它应该显示所有行,而不是单个结果。在这个函数上它只返回第一个而不是全部。
希望您的帮助。在此先感谢。
【问题讨论】:
标签: php function line execution