A question about using a text field (TextArea)

Source: Internet
Author: User
Tags ini modify string format
Problem I found out that the text field was handled incorrectly when I used the form to process information. For example, one of the following tables:

---------------------------------------------------------------
Test.html

<title> TextArea Test </title>
<body bgcolor= "#FFFFFF" >
<form method= "POST" action= "test.php" >
FileName <input type= "text" name= "filename" value= "test.txt" ><br>
Content:<br>
<textarea name= "Content" cols= "rows=" > "AAA" \ "Bbb\" </textarea><br>
<input type= "Submit" name= "Submit" value= "written" >
<input type= "reset" name= "Submit2" value= "Rewrite" >
</form>
</body>

test.php

?
$fp=fopen ($filename, "w");
Fwrite ($fp, $content);
Fclose ($FP);
echo "OK";
?>
------------------------------------------------------

The above example is used to test, mainly to complete the user can enter a file name, and then can enter the contents of the file. After confirmation, you can save the file on the server. The following is a brief description of the contents of a two file.

There is a form in the test.html with a text box and a text field. The text box is used to enter the name of the file to save, the text field
Used to enter the contents of the file. The filename defaults to "test.txt" and the contents of the file are set to "AAA" \ "Bbb\" by default. The action of the form is "post" and the execution file is "test.php".

Test.php is very simple. Opens the specified file, writes the contents of the file, closes the file, and outputs "OK".

Originally I think the document content should be "AAA" \ "Bbb\", but the result is not so, but the "aaa\" \\\ "bbb\\\"! An escape backslash is added before each double quotation mark (") and backslash (\) (In fact, a single quotation mark and an empty (nul)). Why is that? So, I query the PHP Chinese manual, see the configuration of php.ini about MAGIC_QUOTES_GPC and magic_quotes_runtime instructions, I know is because of the automatic processing of PHP. In this way, I will php.ini the MAGIC_QUOTES_GPC and magic_quotes_runtime in the configuration
Set to OFF, the result is correct.

But what if I can't change the server? Then I looked at the string handler function, and I found that the stripslashes () function would do the job. This first change the php.ini to the original appearance, and then modify the test.php as follows:

--------------------------------------------------
?
$fp=fopen ($filename, "w");
$content=stripslashes ($content);
Fwrite ($fp, $content);
Fclose ($FP);
echo "OK";
?>
--------------------------------------------
Then check the results, fully meet my needs!

Also note that if you want to do an application that modifies the contents of the file. For example, first read the contents of the file, put into the text field, users can modify. Then when there are special symbols in the file, such as "<", ">", "&" and so on, the direct display will be inconsistent with the actual content. Never mind, PHP also provides the Htmlspecialchars () function to convert special characters into HTML string format, so that the display and processing are correct.

This is a problem I found when I was working on a text field and I hope it will help you!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.