You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于PHP实现HTML文件复制与内容替换的表单开发需求

Solution: PHP File Copy & Content Replacement with Form

Got it, let's build this out properly. Below is a complete, self-contained PHP script that handles both the input form and the file manipulation logic. You can drop this into a file (like file-modifier.php) and run it on a PHP-enabled web server.

Full Working Code

<?php
$message = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get form inputs
    $newFilename = trim($_POST['new_filename']);
    $pReplacement = trim($_POST['p_content']);
    $h1Replacement = trim($_POST['h1_content']);
    
    // Validate inputs
    if (empty($newFilename) || empty($pReplacement) || empty($h1Replacement)) {
        $message = '<p style="color: red;">Please fill in all fields!</p>';
    } else {
        $originalFile = 'test.html';
        
        // Check if original file exists
        if (!file_exists($originalFile)) {
            $message = '<p style="color: red;">Original file test.html not found!</p>';
        } else {
            // Read original file content
            $content = file_get_contents($originalFile);
            
            // Replace target content
            $modifiedContent = str_replace(
                ['<p>welcome people</p>', '<h1>nice</h1>'],
                [$pReplacement, $h1Replacement],
                $content
            );
            
            // Write to new file
            $writeSuccess = file_put_contents($newFilename, $modifiedContent);
            
            if ($writeSuccess !== false) {
                $message = "<p style=\"color: green;\">Success! File created as $newFilename</p>";
            } else {
                $message = '<p style="color: red;">Failed to create new file. Check directory permissions!</p>';
            }
        }
    }
}
?>

<!DOCTYPE html>
<html>
<body>
    <h2>Customize and Copy test.html</h2>
    <?php echo $message; ?>
    <form method="POST">
        <div>
            <label for="new_filename">New Filename (e.g., test01.html):</label><br>
            <input type="text" id="new_filename" name="new_filename" required>
        </div>
        <br>
        <div>
            <label for="p_content">Replacement for &lt;p&gt;welcome people&lt;/p&gt;:</label><br>
            <textarea id="p_content" name="p_content" rows="3" required></textarea>
        </div>
        <br>
        <div>
            <label for="h1_content">Replacement for &lt;h1&gt;nice&lt;/h1&gt;:</label><br>
            <textarea id="h1_content" name="h1_content" rows="3" required></textarea>
        </div>
        <br>
        <button type="submit">Create Modified File</button>
    </form>
</body>
</html>

Breakdown of Features

  • Input Form:
    • Three required fields: new filename, replacement content for the <p> tag, and replacement content for the <h1> tag.
    • Uses textareas for content replacements so you can input full HTML snippets (like <p>Hello World!</p> or <h1>My Custom Heading</h1>).
  • PHP Logic:
    • Validates that all fields are filled to avoid empty inputs.
    • Checks if the original test.html exists before proceeding.
    • Uses str_replace() to target and swap the exact content blocks you specified.
    • Shows clear success/error messages to let you know if the file was created properly.
    • Includes handling for common issues like missing original files or directory permission errors.

Important Notes

  • Ensure your web server has write permissions in the directory where this script is located—otherwise, it won't be able to create the new HTML file.
  • The original test.html needs to be in the same directory as this PHP script, or you can update the $originalFile path to point to its correct location.

内容的提问来源于stack exchange,提问作者Alilou Hassani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:14:48