如何在下面提到的代码中用PHP脚本替换文本区域的内容

如何在下面提到的代码中用PHP脚本替换文本区域的内容,php,Php,我已经采取了2个文本框和1个文本区 用户将通过搜索框搜索内容 并规定了应替换为哪个词 场景1: 我想用“坏”一词代替“好” 但此代码不会替换文本区域内容。 它会附加新替换的字符串 解决办法是什么 <body> <form id="form1" name="form1" method="post" action=""> <p> <label for="search">Search :</label> <input type="t

我已经采取了2个文本框和1个文本区 用户将通过搜索框搜索内容 并规定了应替换为哪个词

场景1:

我想用“坏”一词代替“好”
但此代码不会替换文本区域内容。 它会附加新替换的字符串

解决办法是什么

<body>

<form id="form1" name="form1" method="post" action="">

<p>
<label for="search">Search :</label>
<input type="text" name="search" id="search" />
</p>

<p>
<label for="replace">Replace</label>
<input type="text" name="replace" id="replace" />
</p>

<p><Br />
<input type="submit" name="submit" id="submit" value="Submit" />
<label for="textarea"></label>
</p>

<p><br />
<textarea name="textarea" id="textarea" cols="45" rows="5"  >
"Good morning how are you today are you feeling Good.";
<?php

if(isset($_POST["submit"]))
{
     $search = $_POST["search"];
 $replace = $_POST["replace"];
 $textarea = $_POST["textarea"];

 $newtext = str_replace($search,$replace,$textarea);
     echo $newtext;

}

?>
   </textarea>
  </p>
</form>

</body>
</html>


搜索:

代替



“早上好,你今天好吗?你感觉好吗?”;


您需要在PHP中使用条件语句来控制是否显示默认文本。目前,您只是在它后面添加动态文本

<textarea name="textarea" id="textarea" cols="45" rows="5"  >
    <?php
        if(isset($_POST["submit"])) {
            $search = $_POST["search"];
            $replace = $_POST["replace"];
            $textarea = $_POST["textarea"];

            $newtext = str_replace($search,$replace,$textarea);
            echo $newtext;
        } else {
            echo "Good morning how are you today are you feeling Good.";
        }
    ?>
</textarea>

HTML脚本

<html>
<body>
<form action="srch.php" method="post">
Find: <input type="text" name="find" value=><br><br>
Replace: <input type="text" name="replace" ><br><br>
<input type="submit" value="Replace"/><br><br>

<textarea name="maintext" rows="8" cols="80"></textarea>
</form>
</body>
</html>

查找:

替换:



php脚本

<html>
<body>

<?php
$find= $_POST['find'];
$replace= $_POST['replace'];
$text= $_POST['maintext'];
if (isset($find) && isset($replace))
{
$newtext= str_replace($find, $replace, $text);
}
?> 
<form action="" method="post">
Find: <input type="text" name="find" value='<?php echo $find; ?>'/><br><br>
Replace: <input type="text" name="replace" value='<?php echo $replace; ?>'/><br><br>
<input type="submit" value="Replace"/><br><br>

<textarea name="maintext" rows="8" cols="80"><?php echo $newtext; ?></textarea>
</form>

</body>
</html>

查找:

请举例说明这是如何出错的(对于真实数据),您实际上是如何将文本推入文本区域的?这应该可以正常工作。也许可以多展示一些code@shanethehat我已经添加了整个代码