从文本文件中获取Php值

3

我有一个文本文件:

foo: bar
el: macho
bing: bong
cake color: blue berry
mayo: ello

我的目标是,如果我搜索"foo",则返回"bar"(如果我搜索"bing",则应返回"bong")。我尝试实现这个目标的方法是首先搜索文件,返回包含结果的行,将其放入字符串中并删除":"之前的所有内容,然后显示该字符串。

// What to look for
$search = 'bing';
// Read from file
$lines = file('file.txt');
foreach($lines as $line)
{
    // Check if the line contains the string we're looking for, and print if it does
    if(strpos($line, $search) !== false)
        echo $line;

    $new_str = substr($line, ($pos = strpos($line, ',')) !== false ? $pos + 1 : 0);
} 
echo "<br>";
echo "bing should return bong:";
echo $new_str;

但它不起作用。上面只是我尝试过的许多方法中的一个。

来源: 许多 stackoverflow 链接和类似的搜索:
https://www.google.com/search?client=opera&q=php+remove+everything+after
https://www.google.com/search?client=opera&q=php+search+text+file+return+line

我以前问过一个问题,但答案对我来说太“专业”了,我真的需要一个新手证明的解决方案/答案。我一整天都在尝试弄清楚它,但我就是无法让它工作。

编辑: 问题已解决!非常感谢您的时间和帮助,我希望这可能对其他人有用!

5个回答

6

This should work with what you are looking for, I tested it on my server and it seems to fit what you are looking for.

$lines_array = file("file.txt");
$search_string = "bing";

foreach($lines_array as $line) {
    if(strpos($line, $search_string) !== false) {
        list(, $new_str) = explode(":", $line);
        // If you don't want the space before the word bong, uncomment the following line.
        //$new_str = trim($new_str);
    }
}

echo $new_str;

?>


在那个 if 条件语句的末尾使用 break; 可能会很有用。 - ner0

2
我会以这种方式完成它:
foreach($lines as $line)
{
  // explode the line into an array
  $values = explode(':',$line);
  // trim the whitspace from the value
  if(trim($values[1]) == $search)
  {
      echo "found value for ".$search.": ".$values[1];
      // exit the foreach if we found the needle
      break;
  }
} 

1
做得很好,@iWizardPro 更快一些,但我感谢你的帮助和时间! - Mdlc

1
 $search = 'bing';
 // Read from file
 $lines = file('text.txt');

 $linea='';
foreach($lines as $line)
  {
  // Check if the line contains the string we're looking for, and print if it does
  if(strpos($line, $search) !== false) {
  $liner=explode(': ',$line);
  $linea.= $liner[1];
  }

  }

  echo 'Search returned: '. $linea;

解释:- 在循环之前创建了$linea变量,它将包含搜索结果。如果在某一行上找到值,则会将字符串爆炸并生成数组,从数组中获取第二个变量,将其放入搜索结果容器变量中。


1
作为您的数据几乎是 YAML [查看lint],您可以使用一个 解析器 来获取相关的 PHP 数组。
但如果您愿意,也可以采用您的解决方案:
 // What to look for
 $search = 'bing';
 // Read from file
 $lines = file('file.txt');
  foreach($lines as $line)
  {
    // Check if the line contains the string we're looking for, and print if it does
    if(strpos($line, $search) !== false){

      echo array_pop(explode(":", $line));

    }

  }

0
使用 fgetcsv
$bits = array();

if (($handle = fopen('t.txt','r')) !== FALSE) {
  while (($data = fgetcsv($handle, 0, ":")) !== FALSE) {
    $bits[$data[0]] = $data[1];
  }
}

# Now, you search

echo $bits['foo'];

$bits将拥有每个分割部分的密钥,这使得您的最终目标非常简单。这就是它的样子:

Array
(
    [foo] =>  bar
    [el] =>  macho
    [bing] =>  bong
    [cake color] =>  blue berry
    [mayo] =>  ello
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接