php - PHP 包含文件、负匹配行、输出文件
问题描述
使用中间 php 文件,我希望调用现有的 php 文件 (get.php)。get.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 removes the line after the line containing keyphrase*/
echo $list; /*Output as m3u file*/
?>
注意:我意识到上面有几个问题。我是初学者水平。
包含文件是 php,但浏览器会下载一个 m3u 播放列表文件。
然后,删除收到的 m3u 文件匹配关键字字符串的行和下一行。
最后,输出将生成的数据作为 m3u 文件(文件名与包含时收到的文件名相同)。
示例文件
This line remains
Remove this line because it has the keyphrase.
Remove this line because it is the line after.
This line also remains
输出文件
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)。如果您只需要一条线,那么上面的内容很棒。如果您需要多行,则需要使用正则表达式 OR 进行匹配或在上述操作之前操作输入文件。
解决方案
在 PHP 中,最好使用preg_replace匹配行并删除它们,而不是尝试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*/
?>
推荐阅读
- c - 如何在缩进代码中添加花括号?
- javascript - Angular 从 firebase 存储中获取 files.length
- javascript - 为什么 getDataRange.getValues() 没有提取所有必需的值?
- node.js - nodejs+mongodb原生驱动+ExpressJS Rest API-一次连接,复用设计建议
- javascript - 如何使用 Ajax 一键提交两个表单
- assembly - NASM:如何正确访问 ssd 驱动器?
- asp.net - 在 Razor Pages 表单中上传文件时出现可选问题
- flutter - 在 Flutter 中添加新文本行的问题
- c# - 如何在 MainLayout 中获取输入参数?
- php - MacOS Laravel Valet 增加 memory_limit