2016-01-20 127 views
0

我正在嘗試使用Json,PHP和jQuery製作帶有可編輯標籤的表單。與Json數據一起使用PHP preg_replace()

我已經從Json文件的問題中讀取了我的PHP表單。

我已經將表單的輸出,序列化爲Json,並將它傳遞給另一個PHP文件。

一旦那裏,我想要我的json字符串,並替換它的位,以便它匹配我原來的json佈局,然後將其寫回到文件。

如果以下說明太長,請跳到步驟5查看實際問題。

步驟1.這裏是被讀入的形式JSON(顯然還有比這更多的問題在真正的文件):

{ 
    "servers": { 
     "questions": [ 
      { 
       "server": "server1", 
       "question":"Is the server running?", 
       "helptext":"It should be", 
       "answers": "Yes, No" 
      }, 
      { 
       "server": "server2", 
       "question":"Is the server running?", 
       "helptext":"It should be", 
       "answers": "Yes, No" 
      } 
     ] 
    } 
} 

第2步:這裏就是我進口文件:

// copy file content into a string var 
$json_file = file_get_contents('data/questions2.json'); 

// convert the string to a json object 
$jfo = json_decode($json_file); 

// read the title value 
//$servers = $jfo->servers->title; 

// copy the posts array to a php var 
$questions = $jfo->servers->questions; 

第3步:這是我的輸出表單域PHP(這樣做是爲了爲每個問題獲得一個數組)。

<?php 
      foreach ($questions as $question) 
      { 
       $id++; 
       echo '<tr>'; 
       echo ' <td>'; 
       echo '  <label for="server'.$id.'">Server</label><br>'; 
       echo '  <input name="question'.$id.'[]" type="hidden" id="serverlabel'.$id.'" value="server"/>'; 
       echo '  <input name="question'.$id.'[]" id="server'.$id.'" value="'.$question->server.'"/>'; 
       echo ' </td>'; 
       echo ' <td>'; 
       echo '  <label for="question'.$id.'">Question '.$id.'</label><br>'; 
       echo '  <input name="question'.$id.'[]" type="hidden" id="questionlabel'.$id.'" value="question"/>'; 
       echo '  <input name="question'.$id.'[]" id="question'.$id.'" size="70" value="'.$question->question.'"/>'; 
       echo ' </td>'; 
       echo ' <td>'; 
       echo '  <label for="helptext'.$id.'">Helptext '.$id.'</label><br>'; 
       echo '  <input name="question'.$id.'[]" type="hidden" id="helptextlabel'.$id.'" value="helptext"/>'; 
       echo '  <input name="question'.$id.'[]" id="helptext'.$id.'" size="70" value="'. $question->helptext . '"/>'; 
       echo ' </td>'; 
       echo ' <td>'; 
       echo '  <label for="answers'.$id.'">Answers to Question '.$id.'</label><br>'; 
       echo '  <input name="question'.$id.'[]" type="hidden" id="answerslabel'.$id.'" value="answers"/>'; 
       echo '  <input class="help" name="question'.$id.'[]" id="answers'.$id.'" value="'.$question->answers.'">'; 
       echo ' <td>'; 
       echo '   <div class="helptext leftpoint">Comma separated list</div>'; 
       echo ' </td>'; 
       echo '</tr>'; 
      } 

       ?> 

第4步:然後我使用jQuery & Ajax的表單數據發佈到其他PHP文件。

我成功轉換的表單數據以JSON字符串如下:

{ 
    "question1": [ 
    "server", 
    "server1", 
    "question", 
    "Is the server running?", 
    "helptext", 
    "It should be", 
    "answers", 
    "Yes, No" 
    ], 
    "question2": [ 
    "server", 
    "server2", 
    "question", 
    "Is the server running?", 
    "helptext", 
    "It should be", 
    "answers", 
    "Yes, No" 
    ] 
} 

第5步:,現在我想作出上述JSON回到我原來的JSON格式,所以我可以把它寫回到這個文件,所以我做了這個preg_replace(),但$ newJson是空的。

//replace numbered questions with single "question" label 
// make hidden form data back into json labels 
$patterns = array(); 
$patterns[0] = '/(question\d)/g'; 
$patterns[1] = '/"server",/'; 
$patterns[2] = '/"question",/'; 
$patterns[3] = '/"helptext",/'; 
$patterns[4] = '/"answers",/'; 
$replacements = array(); 
$replacements[0] = 'question'; 
$replacements[1] = '"server":'; 
$replacements[2] = '"question":'; 
$replacements[3] = '"helptext":'; 
$replacements[4] = '"answers":'; 

$newJson = preg_replace($patterns, $replacements, $json); 

我用這個文檔 http://php.net/manual/en/function.preg-replace.php和美妙的正則表達式驗證工具,https://regex101.com/

什麼我在第5步做錯了什麼?

(抱歉,如果我的問題太長,但希望它能讓您知道我正在嘗試做什麼以及爲什麼,以及由於我的解決方案是從各種其他StackOverflow頁面拼湊而成的,有人)

+1

在PHP中沒有'g'修飾符,因此可能是一個問題。 – chris85

+3

這是一個X/Y問題。爲什麼不讀取客戶端上的JSON並在那裏生成?保存2或3個步驟。原來的JSON甚至來自同一臺服務器,因此沒有原產地問題 – mplungjan

+0

因爲我嘗試使用Angular,無法在Json之後加載進一步的JavaScript事件。 (請參閱許多其他StackOverflow有關在document.onload的Angular等效項上觸發事件的問題以及許多相互衝突的答案)。 –

回答

1

改變這一行:

$patterns[0] = '/(question\d)/g'; 

$patterns[0] = '/(question\d)/'; 

修復您的問題。除了PHP中不存在的PCRE修飾符之外,在這種情況下它是沒有意義的。The documentation說(重點煤礦),

每個模式將被替換對應更換

這樣的preg_replace不會僅僅停留替換它取代問題1問題後。相反,它繼續替換/question \ d/問題的所有實例,這正是Perl(但不是PHP)中修飾符所做的修改。

您可以在PHP沙盒here上看到結果。

+0

非常感謝你,這非常棒。現在我只需要弄清楚如何使Json有效(我使用的是http://www.jsoneditoronline.org/),但是你的正則表達式像夢一樣運行。感謝PHP沙箱鏈接,這太棒了。 –