0

I am trying to make use of files(.txt) to print logs. Inside the file, it logs an array value which looks like this:

  Array
  (
      [NAME] => John Peters
      [AGE] => 24
      [COUNTRY] => United States
      [EMAIL] => test@test.com
  )

So now, I am trying to read the file contents and covert it onto an actual array so that I would be able to reference the value using the array key in a php file, something like:

  echo 'Name : ' .$person['NAME'];
  echo 'Age: ' .$person['AGE'];
  echo 'Country: ' .$person['COUNTRY'];
  echo 'Email: ' .$person['EMAIL'];

Is there a predefined php function to do it? Or how will I be able to accomplish what I want. I have tried to use the fread() and fgets() function but it doesn't really accomplish what I want or I might be missing something.

4

4 回答 4

2

我为您编写了一个快速脚本,我假设在您的 (files).txt 中可以包含许多print_r结果条目,例如

Array
  (
      [NAME] => John Peters
      [AGE] => 24
      [COUNTRY] => United States
      [EMAIL] => test@test.com
  )
Array
  (
      [NAME] => John Peters
      [AGE] => 24
      [COUNTRY] => United States
      [EMAIL] => test@test.com
  )

此脚本假定您的输入test.txt仅包含具有 1 级的数组(因此,它不适用于嵌套数组)

$c = file_get_contents('test.txt');

# Matches all strings that has 'Array(...)' pattern
preg_match_all('#Array[^\)]+\)#', $c, $matches);

$items = array();
foreach($matches[0] as $match) {

    # Extracts KEY => VAL patterns from matched text 
    if (preg_match_all('#\[([^\]]+)\].*?>(.*)#', $match, $array)) {
        $items[] = array_combine($array[1], $array[2]);
    }
}

# test your results
print_r($items);
于 2013-08-22T02:28:13.613 回答
0

您可以使用file_get_contents阅读它

例如:

<?php
$homepage = file_get_contents('abc.txt');
echo $homepage;
?>

希望它会有所帮助:)

于 2013-08-22T02:05:14.330 回答
0

我猜@Rezigned 和我有同样的想法......这就是我想出的:

<?php
  $file = file_get_contents('log.txt');
  $file = preg_match_all('/(\s+\[[a-zA-Z0-9]+\]\s+=>\s+.+)\n/', $file, $lines);
  $key = array();
  $val = array();
  foreach ($lines[0] as $line) {
    $keys_vals = preg_split('/(\s+=>\s+)/', $line);
    $key[] .= preg_replace('/[\[|\]]/', '', $keys_vals[0]);
    $val[] .= $keys_vals[1];
  }
  $line_count = count($lines[0]);
  for ($i = 0; $i < $line_count; $i++) {
    print $key[$i] . ': ' . $val[$i];
  }
于 2013-08-22T03:11:36.653 回答
0

在 PHP 手册中有一个 Matt 共享的函数print_r_reverse,我想这就是你想要的。以下代码print_r直接从 PHP 手动函数注释部分复制。

<?php 
function print_r_reverse($in) { 
    $lines = explode("\n", trim($in)); 
    if (trim($lines[0]) != 'Array') { 
        // bottomed out to something that isn't an array 
        return $in; 
    } else { 
        // this is an array, lets parse it 
        if (preg_match("/(\s{5,})\(/", $lines[1], $match)) { 
            // this is a tested array/recursive call to this function 
            // take a set of spaces off the beginning 
            $spaces = $match[1]; 
            $spaces_length = strlen($spaces); 
            $lines_total = count($lines); 
            for ($i = 0; $i < $lines_total; $i++) { 
                if (substr($lines[$i], 0, $spaces_length) == $spaces) { 
                    $lines[$i] = substr($lines[$i], $spaces_length); 
                } 
            } 
        } 
        array_shift($lines); // Array 
        array_shift($lines); // ( 
        array_pop($lines); // ) 
        $in = implode("\n", $lines); 
        // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one) 
        preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); 
        $pos = array(); 
        $previous_key = ''; 
        $in_length = strlen($in); 
        // store the following in $pos: 
        // array with key = key of the parsed array's item 
        // value = array(start position in $in, $end position in $in) 
        foreach ($matches as $match) { 
            $key = $match[1][0]; 
            $start = $match[0][1] + strlen($match[0][0]); 
            $pos[$key] = array($start, $in_length); 
            if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1; 
            $previous_key = $key; 
        } 
        $ret = array(); 
        foreach ($pos as $key => $where) { 
            // recursively see if the parsed out value is an array too 
            $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0])); 
        } 
        return $ret; 
    } 
} 
于 2013-08-22T03:22:57.693 回答