我已经更正了一个代码,仅应从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
现在我要确定,
希望您的帮助。在此先感谢。
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);