【发布时间】:2020-04-30 12:53:03
【问题描述】:
我有一个 txt 文件,我已将其作为数组读取(txt 文件中的每一行作为数组元素)。我只对以“关键字:”开头的数组元素感兴趣。我确实需要提取单词“Keywords:”之后的所有单词并将它们保存在另一个数组中。为了搜索我使用 foreach 循环的所有 txt 文件。但我不知道如何获取“关键字:”之后的字符串。
txt文件示例:
xxxxxxxx xxxxxxx xxxxxxxx 关键词:风化;光氧化;稳定;聚丙烯;南极洲; HALS xxxxxxxxxxxxx xxxxxxxxxxxxx 关键词:南极洲;测速仪;降雪率;雷达;不确定性量化
我希望最终数组仅包含“风化;光氧化;稳定化;聚丙烯;南极洲;HALS;南极洲;Disdrometer;降雪率;雷达;不确定性量化
我使用的代码:
<?php
// Get a file into an array.
// the file saved at C:\Tamer\Open Polar\New Keywords\Original citations files\combined.txt
$lines = file('C:\Tamer\Open Polar\New Keywords\Original citations files\combined.txt');
// Loop through our array, show HTML source as HTML source.
foreach ($lines as $line_num => $line) {
$key = strstr($line, ' ', true);
if ($key = "Keywords:"){
// I do not know how to get the string after the "Keyword:"
echo "$key" . "<br />\n";
}
}
?>
有人可以帮忙吗??
【问题讨论】:
-
我们知道您想要的字符串在“关键字:”之后开始,但您没有告诉我们该字符串在哪里停止。在行尾?
-
是的 - 行以“关键字:”开头,后跟字符串(我需要)并在行尾结束。注意每一行都保存为数组元素