代码之家  ›  专栏  ›  技术社区  ›  Alilou Hassani

HTML+表单+输入+REMPLACE代码+PHP

  •  -2
  • Alilou Hassani  · 技术社区  · 6 年前

    我有一个名为“test.html”的文件,其代码为:

    <html>
        <body>
            <p>welcome people<p>
            <h1>nice</h1>
        </body>
    </html>
    

    我需要创建“test.html”的副本并替换:

    <p>welcome people<p>
    

    <h1>nice</h1>
    

    使用另一个代码。

    因此,我需要创建一个包含3个输入的表单:

    • 输入1:副本的名称(expemple test01.html)
    • 输入2:替换的代码 <p>welcome people</p>
    • 输入3:替换的代码 <h1>nice</h1>

    我需要跟踪 form 在名为(control.html)的文件中,代码为:

    <html>
        <body>
            <form method="get" action="process.php">
                <input name="titlecopyfile">
                <input name="code1">
                <input name="code2">
                <input type="submite">
            </form>
        </body>
    </html>
    

    我需要以下代码:

    • 测验html
    • 控制html
    • 过程php
    1 回复  |  直到 6 年前
        1
  •  -1
  •   d.coder    6 年前

    以下是解决方案:

    测验html:

    <html>
        <body>
            <p>welcome people<p>
            <h1>nice</h1>
        </body>
    </html>
    

    控制html:

    <html>
        <body>
            <form method="post" action="process.php">
                <input name="titlecopyfile">
                <input name="code1">
                <input name="code2">
                <input type="submit">
            </form>
        </body>
    </html>
    

    过程php:

    <?php
    // Get all request parameters
    $filename = $_REQUEST['titlecopyfile'];
    $code1 = $_REQUEST['code1'];
    $code2 = $_REQUEST['code2'];
    
    // Read existing file content in a variable
    $testHtml = file_get_contents('test.html');
    
    // Replace desired strings with actual values
    $testHtml = str_replace(array('<p>welcome people<p>', '<h1>nice</h1>'), array($code1, $code2), $testHtml);
    
    // Write file on disk physically and save it as specified name
    file_put_contents($filename, $testHtml);