PHP包括文件、负匹配线、输出文件
使用中间php文件,我希望调用现有的php文件(get.php)。php输出一个m3u(文本)文件。在将m3u文件发送到浏览器文件之前,我需要删除某些行PHP包括文件、负匹配线、输出文件,php,awk,grep,Php,Awk,Grep,使用中间php文件,我希望调用现有的php文件(get.php)。php输出一个m3u(文本)文件。在将m3u文件发送到浏览器文件之前,我需要删除某些行 <?php ob_start(); include('get.php'); /*Receives an m3u file*/ $output = ob_get_contents(); $list = preg_replace('Remove.*keyphrase.*\n.*\n' , '' , $output ); /*Also remo
<?php
ob_start();
include('get.php'); /*Receives an m3u file*/
$output = ob_get_contents();
$list = preg_replace('Remove.*keyphrase.*\n.*\n' , '' , $output ); /*Also removes the line after the line containing keyphrase*/
echo $list; /*Output as m3u file*/
?>
输出文件
This line remains
This line also remains
这就是我最终使用的:
<?php
$file = file('http://'.$_SERVER['HTTP_HOST'].'/get.php?'.$_SERVER['QUERY_STRING']);
$list = preg_grep("/keyphrase/",$file,PREG_GREP_INVERT);
// to have Array
//print_r($list);
// back to string
echo implode("",$list);
?>
我无法使preg_grep()与最基本的测试匹配两行(即使使用/s或/m)。如果你只需要一行,那么上面的内容就很好了。如果您需要多行代码,则需要使用regex进行匹配,或在上述操作之前操作输入文件。在PHP中,最好使用匹配行并删除它们,而不是尝试
shell
->grep
您的问题没有足够的信息说明您希望如何匹配要删除的线的模式
编辑
请尝试以下代码:
<?php
ob_start();
include('get.php'); /*Receives an m3u file*/
$output = ob_get_contents();
$list = preg_replace('/Remove.*keyphrase.*\n.*\n/' , '' , $output ); /*Also removes the line after the line containing keyphrase*/
echo $list; /*Output as m3u file*/
?>
您可以使用并将整个文件读取到一个数组中
如果设置为PREG\u GREP\u INVERT
,此函数将返回与给定模式不匹配的输入数组元素
[akshay@c1 tmp]$ cat get.php
This line remains
Remove this line because it has the keyphrase.
Remove this line because it is the line after the keyphrase.
This line also remains
[akshay@c1 tmp]$ grep -v keyphrase get.php
This line remains
This line also remains
[akshay@c1 tmp]$ cat test.php
<?php
$file = file('get.php');
$list = preg_grep("/keyphrase/",$file,PREG_GREP_INVERT );
// to have Array
print_r($list);
// back to string
echo implode("",$list);
?>
[akshay@c1 tmp]$ php test.php
Array
(
[0] => This line remains
[3] => This line also remains
)
This line remains
This line also remains
grep
需要来自stdin或文件的数据。谢谢。它现在更新为preg_replace(),并尝试匹配示例输入。我更新了答案。现在输出与预期一致。谢谢!你的-v匹配有效,因为我愚蠢地在第2行和第3行中都放了这个词。我已经调整了样本文件的大小。还有,多亏了罗恩,然后是你,我在问题中试探了一下preg_替换。另一件事是这个。当我调用test.php文件时,我将在URL中传递参数。调用get using file()时,这些参数是否会通过?当我使用include时,这些值传递到get.php。@dyna在当前上下文中get.php
只是文件名,但是file()
也支持file('http://www.example.com?par1=foo&par2=bar)
,请在@AkshayHedge上阅读我实现的代码。它在浏览器中显示get.php文件的源代码。我意识到这可能并不清楚,所以我将再次重申。当您从浏览器调用get.php?foo=bar&goo=car时,浏览器会下载一个扩展名为m3u8的文件。此文件未在屏幕上打开。我需要在这个文件中添加替换,然后下载。@AkshayHedge我感谢你的帮助。如果时间允许,我仍在做这件事。我现在将此代码用于file():$file=file('http://'.$\u SERVER['http\u HOST']./get.php?'.$\u SERVER['QUERY\u STRING'])代码>现在php得到处理。现在我正在和preg_grep一起研究正则表达式。我看到了阵列号。如果我只匹配一行(更少的正则表达式比较),删除它,然后按数组号删除下面的行,可能会更有效。超出我的工资等级。我将继续摆弄preg_grep regex,尝试同时匹配两行并删除。@dyna很高兴知道这一点
[akshay@c1 tmp]$ cat get.php
This line remains
Remove this line because it has the keyphrase.
Remove this line because it is the line after the keyphrase.
This line also remains
[akshay@c1 tmp]$ grep -v keyphrase get.php
This line remains
This line also remains
[akshay@c1 tmp]$ cat test.php
<?php
$file = file('get.php');
$list = preg_grep("/keyphrase/",$file,PREG_GREP_INVERT );
// to have Array
print_r($list);
// back to string
echo implode("",$list);
?>
[akshay@c1 tmp]$ php test.php
Array
(
[0] => This line remains
[3] => This line also remains
)
This line remains
This line also remains
[akshay@c1 tmp]$ cat test-shell.php
<?php
$list = shell_exec('grep -v "keyphrase" get.php');
echo $list;
?>
[akshay@c1 tmp]$ php test-shell.php
This line remains
This line also remains