如何修改此 php 函数以在结果执行前检查所有行?
How to modify this php function to check all lines before result execution?
我已经更正了一个代码,它应该只回显所有 'lines' 包含单词 "TrxID",来自 file.html。但它是'paragraph'中的第一个returns,其中包含"TrxID",因此有超过3行包含"TrxID"。
File.html
<html lang="en"> <body> <p> recieved 0<br /> to 862444947 successful<br /> Fee $ 9<br />25<br /> Balance Tk 00<br />78<br /> TrxID 6DU2ATVQCO at 30/04/2019 19:25<br />download the app </p>
<p> recieved 0<br /> to 862444947 successful<br /> Fee $ 9<br />25<br /> Balance Tk 00<br />78<br /> Tr 6DU2ATVQCO at 30/04/2019 19:25<br />download the app </p>
<p> recieved 0<br /> to 862444947 successful<br /> Fee $ 9<br />25<br /> Balance Tk 00<br />78<br /> TrxID 6DU2ATVQgfCO at 30/04/2019 19:25<br />download the app </p>
<p> recieved 0<br /> to 862444947 successful<br /> Fee $ 9<br />25<br /> Balance Tk 00<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 0 to 862444947 successful Fee $ 9 25 Balance Tk 00 78 TrxID 6DU2ATVQCO at 30/04/2019 19:25 download the app
我要:
TrxID 6DU2ATVQCO at 30/04/2019 19:25
现在我想确定一下,
- 它应该只有 returns 行包含 'TRXID',而不是段落,如果我用 'paragraph tag' 替换 br/ 结果保持不变
- 它应该显示所有行,而不是单个 result.on 这个函数它只是 returns 第一个而不是所有行。
希望您提前 help.Thanks。
我稍微修改了你的代码:
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('<br />', $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
另一种方法是,您可以将整个文件内容传递给 preg_match,您将在 $matches.
中返回一个匹配项目的数组
$result = preg_match('/TrxID .+ at [\d]{2}\/[\d]{2}\/[\d]{4} [\d]{2}:[\d]{2}/i', $fileContents, $matches)
print_r($matches);
我已经更正了一个代码,它应该只回显所有 'lines' 包含单词 "TrxID",来自 file.html。但它是'paragraph'中的第一个returns,其中包含"TrxID",因此有超过3行包含"TrxID"。
File.html
<html lang="en"> <body> <p> recieved 0<br /> to 862444947 successful<br /> Fee $ 9<br />25<br /> Balance Tk 00<br />78<br /> TrxID 6DU2ATVQCO at 30/04/2019 19:25<br />download the app </p>
<p> recieved 0<br /> to 862444947 successful<br /> Fee $ 9<br />25<br /> Balance Tk 00<br />78<br /> Tr 6DU2ATVQCO at 30/04/2019 19:25<br />download the app </p>
<p> recieved 0<br /> to 862444947 successful<br /> Fee $ 9<br />25<br /> Balance Tk 00<br />78<br /> TrxID 6DU2ATVQgfCO at 30/04/2019 19:25<br />download the app </p>
<p> recieved 0<br /> to 862444947 successful<br /> Fee $ 9<br />25<br /> Balance Tk 00<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 0 to 862444947 successful Fee $ 9 25 Balance Tk 00 78 TrxID 6DU2ATVQCO at 30/04/2019 19:25 download the app
我要:
TrxID 6DU2ATVQCO at 30/04/2019 19:25
现在我想确定一下,
- 它应该只有 returns 行包含 'TRXID',而不是段落,如果我用 'paragraph tag' 替换 br/ 结果保持不变
- 它应该显示所有行,而不是单个 result.on 这个函数它只是 returns 第一个而不是所有行。
希望您提前 help.Thanks。
我稍微修改了你的代码:
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('<br />', $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
另一种方法是,您可以将整个文件内容传递给 preg_match,您将在 $matches.
中返回一个匹配项目的数组$result = preg_match('/TrxID .+ at [\d]{2}\/[\d]{2}\/[\d]{4} [\d]{2}:[\d]{2}/i', $fileContents, $matches)
print_r($matches);