I have corrected a code,whice should only echo all 'lines' containing word "TrxID",from file.html. but it is returns first of the 'paragraph' amongst them whice contains "TrxID", thus there are more than 3 lines contains "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; ?>
Result
recieved $500 to 862444947 successful Fee $ 9 25 Balance Tk $1000 78 TrxID 6DU2ATVQCO at 30/04/2019 19:25 download the app
I want :
TrxID 6DU2ATVQCO at 30/04/2019 19:25
Now I want to make sure,
Hoping your help.Thanks in Advance.
I modified your code a little:
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
or if you want to echo it :
echo $output = implode('<br />', $result);
If you want just string after 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