【问题标题】:Get the string after a certain word from array elements从数组元素中获取某个单词后的字符串
【发布时间】: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";

            }


}
?>

有人可以帮忙吗??

【问题讨论】:

  • 我们知道您想要的字符串在“关键字:”之后开始,但您没有告诉我们该字符串在哪里停止。在行尾?
  • 是的 - 行以“关键字:”开头,后跟字符串(我需要)并在行尾结束。注意每一行都保存为数组元素

标签: php arrays string


【解决方案1】:

如果字符串以Keywords: 开头,并且您想将后面的内容添加到数组中,则一种选择是使用例如substr

$lines = file('C:\Tamer\Open Polar\New Keywords\Original citations files\combined.txt', FILE_IGNORE_NEW_LINES);
$finalArray = [];
foreach ($lines as $line_num => $line) {
    if (substr( $line, 0, 9 ) === "Keywords:") {
        $finalArray[] = substr($line, 10);
    }
}

var_export($finalArray);

输出

array (
  0 => 'Weathering; Photo-oxidation; Stabilization; Polypropylene; Antarctica; HALS',
  1 => 'Antarctica; Disdrometer; Snowfall rate; Radar; Uncertainty quantification',
)

【讨论】:

    【解决方案2】:

    如果您只需要在“关键字”之后获取 字符串,您可以从this SO answer 获取解决方案。

    所以在你的情况下,它可能看起来像这样:

    $needle = 'Keyword:'; 
    
    // string after needle
    $result = substr($line, strpos($line, $needle) + strlen($needle));
    

    【讨论】:

      【解决方案3】:

      这个例子很简单,实际上只使用explode() 函数添加

      $lines = "
      xxxxxxxx xxxxxxx xxxxxxxx Keywords: Weathering; Photo-oxidation; Stabilization; Polypropylene; Antarctica; HALS
      xxxxxxxxxxxxx xxxxxxxxxxxxx Keywords: Antarctica; Disdrometer; Snowfall rate; Radar; Uncertainty quantification
      This line does not have any
      xxxx xxxxxx xxxxxx Keywords: some more; keywords";
      $linesArr = explode("\n",$lines);
      $keywords = array();
      foreach ($linesArr as $line_num => $line) {
        $lineArr = explode("Keywords:",$line);
        if (array_key_exists(1,$lineArr)){
          $lineKeywords = explode('; ',$lineArr[1]);
          foreach ($lineKeywords as $keyword){
            $keywords[] = $keyword;
          }   
        }
      }
      var_dump($keywords);
      

      【讨论】:

        猜你喜欢
        • 2021-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-01
        • 1970-01-01
        • 2018-09-19
        相关资源
        最近更新 更多