PHP - Convert textarea submitted
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
add a comment |
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
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
add a comment |
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
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
php
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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;
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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
add a comment |
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;
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
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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