Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php从日志文件中删除与某个字符串值匹配的行_Php - Fatal编程技术网

php从日志文件中删除与某个字符串值匹配的行

php从日志文件中删除与某个字符串值匹配的行,php,Php,我是php新手。我正在尝试从日志文件中删除与某个字符串值匹配的行 下面的代码可以显示日志文件中匹配的行,但我想删除该行并附加日志文件的其余行 下面是我的代码: <?php $searchthis = "mystring"; $matches = array(); $handle = @fopen("myfile.log", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($hand

我是php新手。我正在尝试从日志文件中删除与某个字符串值匹配的行

下面的代码可以显示日志文件中匹配的行,但我想删除该行并附加日志文件的其余行

下面是我的代码:

<?php
$searchthis = "mystring";
$matches = array();

$handle = @fopen("myfile.log", "r");
if ($handle)
{
    while (!feof($handle))
    {
        $buffer = fgets($handle);

        if(strpos($buffer, $searchthis) !== FALSE)
            $matches[] = $buffer;
    }
    fclose($handle);
}

//show results:
print_r($matches);
?>
john abraham hml3
john abraham hml1
$search\u string='hml3'

函数

function search_and_delete($_file,$search_string){
        $file = file($_file);
        foreach ($file as $line_index => $line) {
            if(strpos($line, $search_string) !== false){
                unset($file[$line_index]);
                break;
            }
        }       
        file_put_contents($_file, implode("",$file));
}
样本使用

search_and_delete("/var/a.txt","removing_line_conteins");
作用

function search_and_delete($_file,$search_string){
        $file = file($_file);
        foreach ($file as $line_index => $line) {
            if(strpos($line, $search_string) !== false){
                unset($file[$line_index]);
                break;
            }
        }       
        file_put_contents($_file, implode("",$file));
}
样本使用

search_and_delete("/var/a.txt","removing_line_conteins");
作用

function search_and_delete($_file,$search_string){
        $file = file($_file);
        foreach ($file as $line_index => $line) {
            if(strpos($line, $search_string) !== false){
                unset($file[$line_index]);
                break;
            }
        }       
        file_put_contents($_file, implode("",$file));
}
样本使用

search_and_delete("/var/a.txt","removing_line_conteins");
作用

function search_and_delete($_file,$search_string){
        $file = file($_file);
        foreach ($file as $line_index => $line) {
            if(strpos($line, $search_string) !== false){
                unset($file[$line_index]);
                break;
            }
        }       
        file_put_contents($_file, implode("",$file));
}
样本使用

search_and_delete("/var/a.txt","removing_line_conteins");

制作文件行数组并选择所需内容

$searchthis = "mystring";
$matches = array();

$handle = file("myfile.log");
foreach ($handle as $buffer)
        if(strpos($buffer, $searchthis) === FALSE)
            $matches[] = $buffer;

//show results:
print_r($matches);

制作文件行数组并选择所需内容

$searchthis = "mystring";
$matches = array();

$handle = file("myfile.log");
foreach ($handle as $buffer)
        if(strpos($buffer, $searchthis) === FALSE)
            $matches[] = $buffer;

//show results:
print_r($matches);

制作文件行数组并选择所需内容

$searchthis = "mystring";
$matches = array();

$handle = file("myfile.log");
foreach ($handle as $buffer)
        if(strpos($buffer, $searchthis) === FALSE)
            $matches[] = $buffer;

//show results:
print_r($matches);

制作文件行数组并选择所需内容

$searchthis = "mystring";
$matches = array();

$handle = file("myfile.log");
foreach ($handle as $buffer)
        if(strpos($buffer, $searchthis) === FALSE)
            $matches[] = $buffer;

//show results:
print_r($matches);



嘿,我如何删除该行并附加其余行?
$matches
仅包含选定行。是的,我了解了您的代码,但我想知道如何从日志文件中删除该行并自动附加其余行?那么,我该如何实现这一点呢?即,您希望修改日志文件,但不停止附加它?我现在还没准备好回答。可能是,你用常识提出了另一个问题?:)嘿,我如何删除该行并附加其余行?
$matches
仅包含选定行。是的,我了解了您的代码,但我想知道如何从日志文件中删除该行并自动附加其余行?那么,我该如何实现这一点呢?即,您希望修改日志文件,但不停止附加它?我现在还没准备好回答。可能是,你用常识提出了另一个问题?:)嘿,我如何删除该行并附加其余行?
$matches
仅包含选定行。是的,我了解了您的代码,但我想知道如何从日志文件中删除该行并自动附加其余行?那么,我该如何实现这一点呢?即,您希望修改日志文件,但不停止附加它?我现在还没准备好回答。可能是,你用常识提出了另一个问题?:)嘿,我如何删除该行并附加其余行?
$matches
仅包含选定行。是的,我了解了您的代码,但我想知道如何从日志文件中删除该行并自动附加其余行?那么,我该如何实现这一点呢?即,您希望修改日志文件,但不停止附加它?我现在还没准备好回答。可能是,你用常识提出了另一个问题?:)嘿,这是从日志文件中删除每一行。我的意思是,一旦我引用页面,它每次都会从日志文件中删除每一行,而不是完美地查找特定字符串。它只删除包含$search\u字符串的第一行。怎么了?是的,它删除了那一行。但是如果你刷新页面,其他顶行就会被删除。再次刷新,即使与字符串不匹配,也会删除另一个top。$search\u字符串变量有什么?我刚刚更新了帖子中的日志行。你能在上面的帖子里检查一下下面的“编辑”吗?嘿,它删除了日志文件中的每一行。我的意思是,一旦我引用页面,它每次都会从日志文件中删除每一行,而不是完美地查找特定字符串。它只删除包含$search\u字符串的第一行。怎么了?是的,它删除了那一行。但是如果你刷新页面,其他顶行就会被删除。再次刷新,即使与字符串不匹配,也会删除另一个top。$search\u字符串变量有什么?我刚刚更新了帖子中的日志行。你能在上面的帖子里检查一下下面的“编辑”吗?嘿,它删除了日志文件中的每一行。我的意思是,一旦我引用页面,它每次都会从日志文件中删除每一行,而不是完美地查找特定字符串。它只删除包含$search\u字符串的第一行。怎么了?是的,它删除了那一行。但是如果你刷新页面,其他顶行就会被删除。再次刷新,即使与字符串不匹配,也会删除另一个top。$search\u字符串变量有什么?我刚刚更新了帖子中的日志行。你能在上面的帖子里检查一下下面的“编辑”吗?嘿,它删除了日志文件中的每一行。我的意思是,一旦我引用页面,它每次都会从日志文件中删除每一行,而不是完美地查找特定字符串。它只删除包含$search\u字符串的第一行。怎么了?是的,它删除了那一行。但是如果你刷新页面,其他顶行就会被删除。再次刷新,即使与字符串不匹配,也会删除另一个top。$search\u字符串变量有什么?我刚刚更新了帖子中的日志行。你可以在上面的帖子中查看下面的“编辑”吗。