title: Using PHP preg_replace() with Json data
body: <p>I am trying to make a form with editable labels using Json, PHP, and jQuery.</p>
  
  <p>I have got my PHP form reading in the questions from the Json file.</p>
  
  <p>I have taken the output of the form, serialized it as Json, and passed it to another PHP file.</p>
  
  <p>Once there, I want to take my json string, and replace bits of it so that it matches my original json layout, and then write it back to the file. </p>
  
  <p><strong>If the below explanation is too long, jump to Step 5 for the actual problem.</strong></p>
  
  <p><strong>Step 1.</strong> Here is the json that gets read into the form (obviously there are more questions than this in the real file):</p>
  
  <pre><code>{
      "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"
              }
          ]
      }
  }
  </code></pre>
  
  <p><strong>Step 2.</strong> Here's where I import the file:</p>
  
  <pre><code>// 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-&gt;servers-&gt;title;
  
  // copy the posts array to a php var
  $questions = $jfo-&gt;servers-&gt;questions;
  </code></pre>
  
  <p><strong>Step 3.</strong> This is my PHP for outputting the form fields (done like this so as to get an array for each question).</p>
  
  <pre><code>&lt;?php 
              foreach ($questions as $question) 
              {
                  $id++;
                  echo '&lt;tr&gt;';
                  echo '  &lt;td&gt;';
                  echo '      &lt;label for="server'.$id.'"&gt;Server&lt;/label&gt;&lt;br&gt;';
                  echo '      &lt;input name="question'.$id.'[]" type="hidden" id="serverlabel'.$id.'" value="server"/&gt;';
                  echo '      &lt;input name="question'.$id.'[]" id="server'.$id.'" value="'.$question-&gt;server.'"/&gt;';
                  echo '  &lt;/td&gt;';
                  echo '  &lt;td&gt;';
                  echo '      &lt;label for="question'.$id.'"&gt;Question '.$id.'&lt;/label&gt;&lt;br&gt;';
                  echo '      &lt;input name="question'.$id.'[]" type="hidden" id="questionlabel'.$id.'" value="question"/&gt;';
                  echo '      &lt;input name="question'.$id.'[]" id="question'.$id.'" size="70" value="'.$question-&gt;question.'"/&gt;';
                  echo '  &lt;/td&gt;';
                  echo '  &lt;td&gt;';
                  echo '      &lt;label for="helptext'.$id.'"&gt;Helptext '.$id.'&lt;/label&gt;&lt;br&gt;';
                  echo '      &lt;input name="question'.$id.'[]" type="hidden" id="helptextlabel'.$id.'" value="helptext"/&gt;';
                  echo '      &lt;input name="question'.$id.'[]" id="helptext'.$id.'" size="70" value="'. $question-&gt;helptext . '"/&gt;';
                  echo '  &lt;/td&gt;';
                  echo '  &lt;td&gt;';
                  echo '      &lt;label for="answers'.$id.'"&gt;Answers to Question '.$id.'&lt;/label&gt;&lt;br&gt;';
                  echo '      &lt;input name="question'.$id.'[]" type="hidden" id="answerslabel'.$id.'" value="answers"/&gt;';
                  echo '      &lt;input class="help" name="question'.$id.'[]" id="answers'.$id.'" value="'.$question-&gt;answers.'"&gt;';
                  echo '  &lt;td&gt;';
                  echo '          &lt;div class="helptext leftpoint"&gt;Comma separated list&lt;/div&gt;';
                  echo '  &lt;/td&gt;';
                  echo '&lt;/tr&gt;';
              }
  
                  ?&gt;  
  </code></pre>
  
  <p><strong>Step 4.</strong> I then use jQuery &amp; Ajax to post the form data to the other PHP file.</p>
  
  <p>I successfully converted the form data to a Json string as follows:</p>
  
  <pre><code>{
    "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"
    ]
  }
  </code></pre>
  
  <p><strong>Step 5.</strong> And now I want to make the above json back into my original json format so I can write it back to the file, so I did this preg_replace(), but $newJson is empty.</p>
  
  <pre><code>//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);
  </code></pre>
  
  <p>I used this documentation
  <a href="http://php.net/manual/en/function.preg-replace.php" rel="nofollow">http://php.net/manual/en/function.preg-replace.php</a> and the wonderful regex validation tool, <a href="https://regex101.com/" rel="nofollow">https://regex101.com/</a></p>
  
  <p><strong>What am I doing wrong in Step 5?</strong></p>
  
  <p>(sorry if my question is too long, but hopefully it will give you an idea of what I am trying to do and why, and also, as my solution was pieced together from various other StackOverflow pages, it might help someone)</p>