PHP - 转换textarea已提交

时间:2015-08-31 17:35:11

标签: php

我有像È™这样需要更换的坏人物。我有99%的代码准备好但有一件事我不知道...我不知道如何从textarea转换内容,然后用好的字符替换它

更准确地说,下面的脚本用于显示文本,我需要的是用好的版本替换该文本

// define variables and set to empty values
$comment = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["comment"])) {
     $comment = "";
   } else {
     $comment = test_input($_POST["comment"]);
   }
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

    <h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
   <textarea class="comment" id="comment" name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
   <br><br>
   <input type="submit" name="submit" value="Submit">
</form>


echo "<h2>Your Input:</h2>";


$ToReplace = array("ă", "î", "È›", "ÅŸ", "Å£", "È™", "â", "ÃŽ", "î", "Î", "â", "ÇŽ", "“", "”", "Ã", "�");
$Replacement   = array("a", "i", "t", "s", "t", "s", "a", "i", "i", "I", "a", "a", "'", "'", "a", "a");
$convert = str_replace($ToReplace, $Replacement, $comment);

echo $convert;

1 个答案:

答案 0 :(得分:0)

如果我确实理解了您的问题您的目标是替换textarea 中的文字。如果是这种情况,只需在$convert内输出textarea变量即可。

正如你的例子所说:

// define variables and set to empty values
$comment = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["comment"])) {
     $comment = "";
   } else {
     $comment = test_input($_POST["comment"]);
   }
}


$ToReplace = array("ă", "î", "È›", "ÅŸ", "Å£", "È™", "â", "ÃŽ", "î", "Î", "â", "ÇŽ", "“", "”", "Ã", "�");
$Replacement   = array("a", "i", "t", "s", "t", "s", "a", "i", "i", "I", "a", "a", "'", "'", "a", "a");
$convert = str_replace($ToReplace, $Replacement, $comment);



function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

    <h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
   <textarea class="comment" id="comment" name="comment" rows="5" cols="40"><?php echo $convert;?></textarea>
   <br><br>
   <input type="submit" name="submit" value="Submit">
</form>


echo "<h2>Your Input:</h2>";
echo $convert;