将 PHP preg_replace() 与 Json 数据一起使用


Using PHP preg_replace() with Json data

我正在尝试使用Json,PHP和jQuery制作具有可编辑标签的表单。

我已经在 Json 文件的问题中阅读了我的 PHP 表单。

我已经获取了表单的输出,将其序列化为 Json,并将其传递给另一个 PHP 文件。

到达那里后,我想获取我的 json 字符串,并替换它的一部分,使其与我的原始 json 布局匹配,然后将其写回文件。

如果下面的解释太长,请跳到实际问题的步骤 5。

第 1 步。这是被读入表单的 json(显然在真实文件中有更多的问题):

{
    "servers": {
        "questions": [
            {
                "server": "server1",
                "question":"Is the server running?",
                "helptext":"It should be",
                "answerswers": "Yes, No"
            },
             {
                "server": "server2",
                "question":"Is the server running?",
                "helptext":"It should be",
                "answerswers": "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="answerswers'.$id.'">Answers to Question '.$id.'</label><br>';
                echo '      <input name="question'.$id.'[]" type="hidden" id="answerswerslabel'.$id.'" value="answerswers"/>';
                echo '      <input class="help" name="question'.$id.'[]" id="answerswers'.$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",
    "answerswers",
    "Yes, No"
  ],
  "question2": [
    "server",
    "server2",
    "question",
    "Is the server running?",
    "helptext",
    "It should be",
    "answerswers",
    "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] = '/"answerswers",/';
$replacements = array();
$replacements[0] = 'question';
$replacements[1] = '"server":';
$replacements[2] = '"question":';
$replacements[3] = '"helptext":';
$replacements[4] = '"answerswers":';
$newJson = preg_replace($patterns, $replacements, $json);

我使用了这个文档http://php.net/manual/en/function.preg-replace.php 和精彩的正则表达式验证工具,https://regex101.com/

我在步骤 5 中做错了什么?

(对不起,如果我的问题太长,但希望它能让你了解我正在尝试做什么以及为什么,而且,由于我的解决方案是从其他各种 StackOverflow 页面拼凑而成的,它可能会帮助某人)

更改此行:

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

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

修复您的问题。 除了 PHP 中不存在的 g PCRE 修饰符之外,它在这种情况下没有意义。 文档说(强调我的),

每个图案将被替换对应物替换

因此,preg_replace不会在用问题替换问题 1 后停止替换。 相反,它继续用 problem 替换/question''d/ 的所有实例,无论如何,g 修饰符在 Perl(但不是 PHP)中都是这样做的。

您可以在此处的 PHP 沙箱中查看结果。