2

I have bad characters like È™ that needs to be replaced. I have 99% of the code prepared but there's one thing I'm missing ... I don't know how to convert content from textarea, and then replace it with the good characters

to be more precise the below script works for displaying the text, what I need is to replace that text with the good version

// 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;
2
  • instead of echo'ing $comment, echo $convert inside the text box. To do that, move your convert code up so it occurs before the textbox. Aug 31, 2015 at 17:42
  • You might want to do a key value pair for your ToReplace and Replacement array and then just use a foreach. Something like "ă" = "a" would be the first value in your $Replacements array. Then foreach($Replacements as $replace => $with){str_replace($replace, $with, $comment); } Aug 31, 2015 at 17:43

1 Answer 1

0

If I did understood your question what you are aiming for is to replace the text inside the textarea. If that's the case is simply a matter of ouputing your $convert variable inside the textarea.

As by your example this works:

// 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;
3
  • thank you, it woks ... a weird thing on the other hand is that it works for some of the characters but it won't work for the others ... am i missing some utf-8 or something ?
    – ccc
    Aug 31, 2015 at 18:13
  • Lter Edit: i was missing utf-8 meta tag on html. everithing works great ... thank you for the help ... i tred giving rep pointss but it seems i'm not allowed to do so...
    – ccc
    Aug 31, 2015 at 18:19
  • @ccc super! ;) Now on the spirit of SO (stackoverflow) if you believe the answer is correct you may upvote the answer and mark it as the solution.
    – Frankie
    Aug 31, 2015 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.