PHP - Convert textarea submitted










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;









share|improve this question
























  • 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.

    – devlin carnate
    Aug 31 '15 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);

    – Joseph Evans
    Aug 31 '15 at 17:43












  • thank you devlin

    – ccc
    Aug 31 '15 at 18:14















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;









share|improve this question
























  • 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.

    – devlin carnate
    Aug 31 '15 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);

    – Joseph Evans
    Aug 31 '15 at 17:43












  • thank you devlin

    – ccc
    Aug 31 '15 at 18:14













2












2








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;









share|improve this question
















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;






php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 2:20









Cœur

18.9k9111153




18.9k9111153










asked Aug 31 '15 at 17:35









cccccc

111




111












  • 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.

    – devlin carnate
    Aug 31 '15 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);

    – Joseph Evans
    Aug 31 '15 at 17:43












  • thank you devlin

    – ccc
    Aug 31 '15 at 18:14

















  • 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.

    – devlin carnate
    Aug 31 '15 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);

    – Joseph Evans
    Aug 31 '15 at 17:43












  • thank you devlin

    – ccc
    Aug 31 '15 at 18:14
















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.

– devlin carnate
Aug 31 '15 at 17:42





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.

– devlin carnate
Aug 31 '15 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);

– Joseph Evans
Aug 31 '15 at 17:43






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

– Joseph Evans
Aug 31 '15 at 17:43














thank you devlin

– ccc
Aug 31 '15 at 18:14





thank you devlin

– ccc
Aug 31 '15 at 18:14












1 Answer
1






active

oldest

votes


















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;





share|improve this answer























  • 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 '15 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 '15 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 '15 at 18:21










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32316792%2fphp-convert-textarea-submitted%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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;





share|improve this answer























  • 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 '15 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 '15 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 '15 at 18:21















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;





share|improve this answer























  • 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 '15 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 '15 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 '15 at 18:21













0












0








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;





share|improve this answer













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;






share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 31 '15 at 17:58









FrankieFrankie

19k1061104




19k1061104












  • 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 '15 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 '15 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 '15 at 18:21

















  • 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 '15 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 '15 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 '15 at 18:21
















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 '15 at 18:13





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 '15 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 '15 at 18:19






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 '15 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 '15 at 18:21





@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 '15 at 18:21



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32316792%2fphp-convert-textarea-submitted%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)