Php get value from text file

2020-02-14 17:22发布

问题:

I have this textfile:

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

And I what I'm trying to accomplish is that if I "look" for foo, it returns bar (if I look for bing, it should return bong). A way a tried to accomplish this is first search though the file, return the line with the result, put it in a string and remove everything before the ":" and display the string.

// 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;

But it doesn't work. Up here is just one of the many things I've tried.

Sources: Many stackoverflow links on and comparable searches:
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

I've asked a question before, but the answers are to "professional" for me, I really need a noob-proof solution/answer. I've been trying to figure it out all day but I just can't get this to work.

Edit: It's solved! Thank you so much for your time & help, I hope this might be useful to someone else to!

回答1:

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;

?>


回答2:

I would do it this way:

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;
  }
} 


回答3:

 $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;

Explanation: - $linea var is created before loop, and it will contain search result. If value is found on line - explode string, and make array, get second var from array, put it in search results container variable.



回答4:

As your data is almost YAML [see lint], you could use a parser in order to get the associated PHP array.

But if can go with your solution as well:

 // 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));

    }

  }


回答5:

Use 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 will have a key for each split part, which makes your ultimate goal quite simple. Here is what it looks like:

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


标签: php