2013-08-22 34 views
0

我正在嘗試使用文件(.txt)來打印日誌。裏面的文件,它記錄看起來像這樣的數組值:有沒有一種簡單的方法來讀取PHP中的數組格式的文件內容?

Array 
    (
     [NAME] => John Peters 
     [AGE] => 24 
     [COUNTRY] => United States 
     [EMAIL] => [email protected] 
) 

所以,現在,我想讀取文件內容和隱蔽它到一個實際的數組,這樣我就可以使用參考值在一個PHP文件中的數組鍵,如:

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

是否有預定義的PHP函數來做到這一點?或者我將如何能夠完成我想要的。我試圖使用fread()和fgets()函數,但它並沒有真正實現我想要的,或者我可能會錯過某些東西。

+4

爲什麼你首先像那樣登錄它?只需記錄JSON,然後將文件解析爲JSON。十分簡單。 –

+0

您是否使用過[** file_get_contents()**](http://php.net/manual/en/function.file-get-contents.php)?另請參閱關於SO的此問答=> http://stackoverflow.com/questions/9529112/json-to-php-array-using-file-get-contents –

+0

您也可以在存儲數據之前對數據使用'serialize()'它到你的日誌文件,然後在加載時使用'unserialize()' – Cyclonecode

回答

0

您可以使用file_get_contents

如閱讀:

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

希望這將有助於:)

+0

這個問題不是讀取文件的實際內容,而是將數據轉換爲適當的值,在這種情況下將其轉換爲數組。 – Cyclonecode

2

我寫了一個快速腳本你, 我認爲你(文件) .txt可以包含許多print_r結果的條目,例如

Array 
    (
     [NAME] => John Peters 
     [AGE] => 24 
     [COUNTRY] => United States 
     [EMAIL] => [email protected] 
) 
Array 
    (
     [NAME] => John Peters 
     [AGE] => 24 
     [COUNTRY] => United States 
     [EMAIL] => [email protected] 
) 

此腳本假定您的輸入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); 
+0

+1就我而言,這是一個很好的答案。 – jerdiggity

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

有一個叫print_r_reverse由Matt PHP手冊中共享的功能,我認爲這是你想要的。以下代碼直接從PHP手冊print_r函數註釋部分複製而來。

<?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; 
    } 
} 
相關問題