【问题标题】:Use PHP to dynamically create an external file that combines user input with pre-existing content使用 PHP 动态创建将用户输入与预先存在的内容相结合的外部文件
【发布时间】:2017-08-27 07:01:16
【问题描述】:

我正在尝试使用 PHP 动态创建一个 .js 文件,该文件将用户输入与预先存在的内容相结合。

简而言之,这样做的原因是我用 Javascript 创建了一个基于 Web 的平台,该平台允许人们阅读基于文本的简短故事,这些故事就像两个或更多人之间的对话一样 - 逐行传递。从技术角度来看,每个故事都使用一个 html 文件和一个相应的 .js 文件,从中提取内容。

我希望我的用户能够创建和分享他们自己的故事。理论上我认为这应该很简单,但遗憾的是,虽然我了解 Javascript,但我对 PHP 知之甚少。

到目前为止,我已经汇总了用户用于构建故事的表单。可以在这里找到一个工作示例:http://damian.me/editor/?name1=John&name2=Mike&name3=Andrew&name4=Roger (我目前正在使用 URL 数据填充表单)

目前提交此表单只是将所有内容放入一个数组中,如下所示:

array(3) {
  ["msgName"]=>
  array(4) {
    [0]=>
    string(4) "John"
    [1]=>
    string(4) "Mike"
    [2]=>
    string(5) "Roger"
    [3]=>
    string(6) "Andrew"
  }
  ["msgContent"]=>
  array(4) {
    [0]=>
    string(34) "Something that john said goes here"
    [1]=>
    string(34) "Something that mike said goes here"
    [2]=>
    string(35) "Something that roger said goes here"
    [3]=>
    string(36) "Something that andrew said goes here"
  }
  ["Leader"]=>
  string(4) "John"
}

我需要做的是找到一种方法来获取这些数据,并将其转换为我的 .js 文件的数据。因此,使用上面的数组,所需的输出将是:

},
'0': {
    'text': "<p><div class=\"user gang p1\"><strong>John</strong><br>\nSomething that john said goes here\n</div>\n<br></p>",
    'passages': {
    },
},
'1': {
    'text': "<p><div class=\"person gang p2\"><strong>Mike</strong><br>\nSomething that mike said goes here\n</div>\n<br></p>",
    'passages': {
    },
},
'2': {
    'text': "<p><div class=\"person gang p2\"><strong>Roger</strong><br>\nSomething that roger said goes here\n</div>\n<br></p>",
    'passages': {
    },
},
'3': {
    'text': "<p><div class=\"person gang p2\"><strong>Andrew</strong><br>\nSomething that andrew said goes here\n</div>\n<br></p>",
    'passages': {
    },

您会注意到 John 的 div 类略有不同,这是因为根据数组,他是“领导者”。因此,除了遍历用户提供的数据之外,还需要某种 IF 语句来捕获它。

总而言之,认识到用户可以根据我的表单提交任意数量的对话行,我如何从数组中获取数据并以上述格式输出(到外部 .js 文件中)?

最终的 .js 文件需要附加页眉和页脚,但我无法自行解决上述数组操作问题。

非常感谢任何帮助。

【问题讨论】:

  • 转储您的输入数据并说明预期结果并不是一个好的 SO 问题。这个问题太笼统了。隔离您的问题并显示您的代码。

标签: php arrays


【解决方案1】:

这是相当冒险的。我为你做了一个简单的例子来实现你想要的。

重要!您需要先清理字符串:http://php.net/manual/en/filter.filters.sanitize.php

现在是代码:

// Your array
$inputArray = array(
    "msgName"=>
    array(0=> "John",
        1=> "Mike",
        2=> "Roger",
        3=> "Andrew",
        ),
    "msgContent"=>
    array(
        0=> "Something that john said goes here",
        1=> "Something that mike said goes here",
        2=> "Something that roger said goes here",
        3=> "Something that andrew said goes here"
    ),
    "Leader"=> "John"
);

// Create the file
$output = fopen("example.js","w");
// a foreach to loop through
foreach($inputArray["msgName"] as $index=>$name)
{
    // We start adding the content
    // You may notice I do not concatinate strings. That is because it uses a lot of memory!
    fwrite($output,"},
'");
    fwrite($output, $index);
    fwrite($output,"': {
    'text': \"<p><div class=\\\"");
    // The if else for the leader
    if ($inputArray["Leader"] == $name)
    {
        fwrite($output,"user gang p1");
    }
    else
    {
        fwrite($output,"person gang p2");
    }

    fwrite($output, "\\\"><strong>");
    fwrite($output, $name);
    fwrite($output, "</strong><br>\\n");
    fwrite($output, $inputArray["msgContent"][$index]);
    fwrite($output, "</div>\\n<br></p>\",
    'passages': {
    },
");
}
// Be sure to close the file
fclose($output);

【讨论】:

  • 这真的很有帮助,谢谢。另外,感谢您指出需要清理输入。我知道这些危险,并会确保在部署之前阅读。
  • Np mate,改变了关于 foreach 工作方式的花絮。取自 mickmackusa 的回答。
  • 很抱歉,我知道这都是初学者的东西,但是我如何使用表单数据作为我的数组,而不是上面使用的硬编码数组?如果我将数据发布到文件中,如何设置 $inputArray = THE FORM DATA
  • 要回答这个问题,我需要知道您如何提交您的阵列。如果你使用表单然后php.net/manual/en/reserved.variables.post.php 否则你可以只做一个php函数并从js调用它:function makeFile($inputArray) {//我的代码除了这里的数组创建}
  • @user5838024 这个问题太宽泛而无法开始,而且正在变得越来越宽泛。你需要做这项工作,只有在你真正陷入困境时才在研究后提出问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多