【问题标题】:PHP - Convert textarea submittedPHP - 转换提交的文本区域
【发布时间】:2015-08-31 17:35:11
【问题描述】:

我有像 È™ 这样的坏字符需要替换。我已经准备好 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;

【问题讨论】:

  • 不是回显$comment,而是在文本框中回显$convert。为此,请将您的转换代码向上移动,使其出现在文本框之前。
  • 你可能想为你的 ToReplace 和 Replacement 数组做一个键值对,然后只使用一个 foreach。像 "ă" = "a" 这样的东西将是 $Replacements 数组中的第一个值。然后 foreach($Replacements as $replace => $with){str_replace($replace, $with, $comment); }

标签: php


【解决方案1】:

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

正如你的例子,这是可行的:

// 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;

【讨论】:

  • 谢谢你,它可以工作了......另一方面,奇怪的是它适用于某些字符,但它不适用于其他字符......我错过了一些 utf- 8 还是什么?
  • Lter 编辑:我在 html 上缺少 utf-8 元标记。一切都很好......谢谢你的帮助......我想给代表点数,但似乎我不允许这样做......
  • @ccc 超级! ;) 现在,根据 SO (stackoverflow) 的精神,如果您认为答案是正确的,您可以投票赞成答案并将其标记为解决方案。
猜你喜欢
  • 1970-01-01
  • 2012-03-16
  • 2023-03-06
  • 2013-12-16
  • 1970-01-01
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多