2015-06-02 55 views
2

我的數據 -的preg_match - 正則表達式來創建陣列

{'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22}} 
{'/Users/aaron/.vim/autoload/timetap.vim': {'total': 0}} 
{'/Users/aaron/.vimrc': {'total': 5}} 
{'/Users/aaron/Documents/Programming/PHP/TimeTapCLI/composer.json': {'total': 144}} 
{'/Users/aaron/Documents/Programming/PHP/TimeTapCLI/timetap.php': {'total': 351}} 
{'/Users/aaron/Box/linux/.vim/autoload/timetap.vim': {'total': 37}} 
{'/Users/aaron/Box/cats.tex': {'total': 184}} 

我試圖創建一個正則表達式,所以我可以在上面轉換成使用的preg_match數組。我想要的數據看起來像 -

我想所有的數據數組所以我相信它應該看起來像如下─

array (
    [0] => array (
     [0] => '/Users/aaron/Box/cats.tex' 
     [1] => array (
        [total] =>'184' 
      ) 
    } 
} 

我的企圖的preg_match -

$subject = file_get_contents('/Users/aaron/.timetap/full.db'); 
$pattern = '{...}'; 
preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE); 

什麼該模式是爲了採取上述數據並將其轉換爲PHP中的數組?有沒有PHP函數可以將其轉換爲數組而不使用preg_match?

回答

3

你的正則表達式是沒有意義的你擁有了它。有一件事你是缺少分隔符。 {,}.都是特殊的正則表達式字符,所以它們應該被轉義。這看起來也像JSON數據結構,因此JSON函數可能對您有用。如果你仍然想要去REGEX,我假設你的數據結構是一致的。

<?php 
$string = "{'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22}} 
{'/Users/aaron/.vim/autoload/timetap.vim': {'total': 0}} 
{'/Users/aaron/.vimrc': {'total': 5}} 
{'/Users/aaron/Documents/Programming/PHP/TimeTapCLI/composer.json': {'total': 144}} 
{'/Users/aaron/Documents/Programming/PHP/TimeTapCLI/timetap.php': {'total': 351}} 
{'/Users/aaron/Box/linux/.vim/autoload/timetap.vim': {'total': 37}} 
{'/Users/aaron/Box/cats.tex': {'total': 184}}"; 
$pattern = '~^\{(.*)\}$~m'; 
$data[] = preg_replace_callback($pattern, function($matches) { 
    global $output_data; 
    preg_match("~'(.*?)'\s*:\s*\{'(.*?)'\s*:\s*(\d+)\}~", $matches[1], $output); 
    $output_data[$output[1]] = array($output[2] => $output[3]); 
}, $string); 
print_r($output_data); 

輸出:

Array 
(
    [/Users/aaron/Applications/developer-vagrant/web/g.php] => Array 
     (
      [total] => 22 
     ) 

    [/Users/aaron/.vim/autoload/timetap.vim] => Array 
     (
      [total] => 0 
     ) 

    [/Users/aaron/.vimrc] => Array 
     (
      [total] => 5 
     ) 

    [/Users/aaron/Documents/Programming/PHP/TimeTapCLI/composer.json] => Array 
     (
      [total] => 144 
     ) 

    [/Users/aaron/Documents/Programming/PHP/TimeTapCLI/timetap.php] => Array 
     (
      [total] => 351 
     ) 

    [/Users/aaron/Box/linux/.vim/autoload/timetap.vim] => Array 
     (
      [total] => 37 
     ) 

    [/Users/aaron/Box/cats.tex] => Array 
     (
      [total] => 184 
     ) 

) 

這裏是鏈接到我用的功能/改性劑的信息。

  1. http://php.net/manual/en/reference.pcre.pattern.modifiers.php
  2. http://php.net/manual/en/function.preg-replace-callback.php
  3. http://php.net/manual/en/function.preg-match.php

我會做一點這裏使用的部分的寫了。如果您有特殊問題,請發帖。

發生了什麼的說明...

~是分隔符,告訴正則表達式引擎,其中表達開始於結束。外部的m是一個修飾符,它告訴它將每行視爲一個字符串。該^$告訴它,因爲m修改的每一行匹配啓動和一個「串」的結束,在這種情況下。在{之前的\是爲了逃避在正則表達式中具有特殊上下文的大括號。 .是任何字符,而*是一個量詞,表示零次或多次出現。當這些配對在一起意味着零個或更多的任何字符。圍繞這個的()是一個捕獲組,它存儲裏面的內容,而\}就是我們停止最後一個花括號。所以從{'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22}}我們結束了'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22}。我們把它傳遞給一個函數,因爲我們想要進一步過濾這個函數。我們使用global這裏是因爲我們這個匿名函數內,並希望當我們done.The '(.*?)'正在尋找單引號之間的一切它是訪問。這被稱爲懶/非貪心,?使其在第一次出現下一個字符(單引號)時停止。 \s*是任何數量的空白。這裏的正則表達式的其餘部分應該可以從前面的描述中辨認出來。該$matches[1]是因爲我們希望首先從分組值preg_replace_callback$matches[0]是被發現的一切(同樣與preg_match)。然後在最後一行,我們將全局變量賦值爲新值。

+0

我正則表達式是完全罰款 - 兄弟。無論如何,由於意思 - 您的輸出不符合要求。 – Falt4rm

+0

What @ Falt4rm ?!我沒有提到你的正則表達式/答案。當我回答時,你的回答並沒有建立一個數組。 – chris85

+0

哦,哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈那麼Mb。我爲此道歉。 – Falt4rm

1

我匹配使用這種模式兩種靶:/(\'.*?\'):\s?\{'.*?(\d{1,})\}/

說明:

  • (\'.*?\') - 第1組:匹配之間炭 '' characteres的任何量」(懶惰)
  • :\s?\{'.*? - 後跟':'和O或1個空格字符和字符'{'以及任意數量的任何字符(懶惰)
  • (\d{1,})\} - 第2組:至少1digits和後跟 '}'

Demo

<?php 
$array_input = 
    array(0 => "{'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22}}", 
      1 => "{'/Users/aaron/.vim/autoload/timetap.vim': {'total': 0}}"); 

$pattern = "/(?:(\'.*?\'):\s?\{'.*?(\d{1,})\})/"; 
$array_output = array(); 

for($i = 0; $i < count($array_input); ++$i) 
{ 
    preg_match($pattern, $array_input[$i], $output); 
    $array_output[$i][0] = $output[1]; 
    $array_output[$i][1] = array('total' => ($output[2])); 
} 

print "<pre>"; 
print_r($array_output); 
print "<pre>"; 
?> 

OUPUT:

Array 
(
[0] => Array 
    (
     [0] => '/Users/aaron/Applications/developer-vagrant/web/g.php' 
     [1] => Array 
      (
       [total] => 22 
      ) 

    ) 

[1] => Array 
    (
     [0] => '/Users/aaron/.vim/autoload/timetap.vim' 
     [1] => Array 
      (
       [total] => 0 
      ) 

    ) 

) 
1

這看起來像它已經在JSON,所以你可以只使用json_decode()把它變成對象。所有你需要做的,以使其兼容PHP的json_decode()是把單個滴答變成雙引號。

$string = "{'/Users/aaron/Applications/developer-vagrant/web/g.php': {'total': 22}}"; 
$string = str_replace("'", '"', $string); 
$object = json_decode($string); 
var_dump($object); 
/* 
Outputs the following: 
object(stdClass)#1 (1) { 
    ["/Users/aaron/Applications/developer-vagrant/web/g.php"]=> 
    object(stdClass)#2 (1) { 
    ["total"]=> 
    int(22) 
    } 
} 
*/