用PHP替换HTML post动作

时间:2017-12-24 15:49:28

标签: php regex

编辑:为了澄清,我只想打开一个文件并更改一些文字。

我有一个带有表单的基本HTML页面,我想用PHP以编程方式更改POST操作。

我有这个PHP脚本,我从另一篇文章中得到了:

<td><strong>Yes</strong>@Html.RadioButtonFor(model => model.isGroupMember , 1, new { Class = "form-control" }, required = "required")</td>
<td><strong>No</strong>@Html.RadioButtonFor(model => model.isGroupMember , 0, new { Class = "form-control" },required = "required")</td>

..和这个HTML文件:

const Profile = require('../UserProfile')
const { defineSupportCode } = require('cucumber')

defineSupportCode(({ defineStep }) => {
   defineStep('A get request is made to the DB', function () {
      return Profile.getDB('http://localhost:5984').then(data => {
        console.log(data)
      })
   })
})

这确实用phpfile.php替换了example.com,但删除了HTML的其他行。这是我在运行PHP脚本后留下的内容:

<?php

    $file = file_get_contents($argv[1]);
    $startPoint='action="';
    $endPoint='"';
    $newText='phpfile.php';
    $newFile = fopen($argv[1], "w");
    fwrite(
        $newFile,
        preg_replace(
            '#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#si',
            '$1'.$newText.'$3',
            $file
        )
    );
    fclose($newFile);
?>

我还没有用PHP编程很长时间,一些帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

首先

更换文件中的字符串的错误想法。解决此问题的一个好方法是使用带有变量的php文件,您可以将其编程定义为要发布的操作文件。

反正

由于这不是问题,我不知道那些#应该是什么意思,但在PCRE(php regex解析器)中,这是固定代码:

preg_replace(
    '/('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')/i',
    '$1'.$newText.'$3',
    $file
)