php - 如何突出显示要下载的文件中的字符串?
问题描述
我有一个文本文件“output.txt”,它是 shell 命令的输出。我想突出显示该文件中作为 $_GET['word'] 的某些单词,然后允许使用 href 下载。
我已经看到了多个问题,但在这种情况下似乎没有一个问题有效。
代码:
$cmd = shell_exec("/usr/local/bin/clustalw2 -infile=input.txt -tree -type=protein -case=upper &");
$file = 'output.txt';
$content = explode("\n",file_get_contents("output.txt"));
$keyword = $_GET['word'];
$content = str_replace($keyword,'<span style="color:red">'.$keyword.'</span>',$content);
$file = file_put_contents($file, $content);
echo "<a href='http://some.thing.com/folder/output.txt'>Download Result file</a>";
它既没有突出显示文本也没有给出任何错误。
解决方案
我怀疑问题在于打开初始文件。以下确实对我有用。
我创建了/home/input.txt:
this is a test
that may
or may not work.
然后跑:
$cmd = shell_exec(" cp /home/input.txt output.txt");
$file = 'output.txt';
$content = explode("\n",file_get_contents("output.txt"));
$keyword = "may";
$content = str_replace($keyword,'<span style="color:red">'.$keyword.'</span>',$content);
$file = file_put_contents($file, implode("\n", $content));
echo "<a href='http://some.thing.com/folder/output.txt'>Download Result file</a>";
output.txt 现在是:
this is a test
that <span style="color:red">may</span>
or <span style="color:red">may</span> not work.
推荐阅读
- java - Java:将 JSON 响应(逐行)添加到 JTable
- database - 如何使用 Oracle dbms_scheduler 每天早上 7:30 到晚上 21:30 每 5 分钟运行一次程序?
- azure - 配置失败。虚拟机“虚拟机名称”的操作系统配置未在分配的时间内完成
- docker - 从 neo4j 3.4.10 将 neo4j 升级到 3.5.3 后,docker-compose 螺栓地址不起作用
- javascript - 在 javascript 中访问响应 Json 数据
- javascript - Webpack jQuery 和 jQuery.lazy() TypeError: q is undefined
- python - operator.ge 行为不一致
- python-3.x - 如何在python中的数据中找到最大匹配模式
- python - Spacy 获取特定单词的位置和标签
- apache-spark - 使用 Gcloud Composer DAG 运行 Spark 作业的困难