PHP textarea text box input content return line (' \ n ') processing method

Source: Internet
Author: User
Tags php server smarty template
Because the line-wrapping in the textarea is "\ n" or "\ r \ n" (Linux is entered as ' \ r \ n ', Windows is ' \ n ') the newline in normal HTML is <br/> or <br>;
Situation one, if the content of the textarea directly into the database, the newline in the database will also be "\ n" or "\ r \ n" Storage
Workaround for this situation:
Method one, (use Smarty template)
1. In the PHP server
$str = Str_replace ("\ r \ n", "<br/>", $value);(or $str = Str_replace ("\ r \ n", "<br>", $value);)
$this->data[' value ']= $str;
2. At the template end
{$value |replace: "&lt;": "<" |replace: "&gt;": ">"}
Method II, (using the Smarty template, this approach is not correct in some frameworks, because these frameworks will be in the server to pass <br>, after the browser parsing will be ' < ' and ' > ' Resolution to ' &lt; ' and ' &gt; ')
1. In the PHP server
$str = Str_replace ("\ r \ n", "<br/>", $value);
$str = Htmlspecialchars_decode ($STR);
$this->data[' value ']= $str;
2. At the template end
{$value}
Method III (no use of smarty template)
1. In the PHP server
$str = Str_replace ("\ r \ n", "<br/>", $value);(or $str = Str_replace ("\ r \ n", "<br>", $value);)
$this->data[' value ']= $str;
2. At the HTML end

<?php Echo Html_entity_decode ($value, ent_quotes);? >//Conversion of special HTML entities (including &lt; and &gt; etc.) back to normal characters (' < ' or ' > ', etc.)


Second, the content of the textarea will be processed before warehousing, and then take the value from the library to do once again processing.


Summary: Situation one and situation two are only two different steps, thinking the same


The following are reproduced others, can be used as a reference,

The workaround is as follows:
I. Form processing
Use JavaScript at commit to replace \ n in textarea with <br>, where jquery is used.
<textarea name= "Test" rows= "3" cols= "a" id= "test" ></textarea>
<script type= "Text/javascript" >
$ ("#form"). Submit (function () {
var Descrip = $ ("#test"). Val ();
Descrip=descrip.replace (/\n/g, ' <br/> ');
$ ("#test"). Val (Descrip);
}
</script>
Two. Service-side processing
Using PHP's built-in function, do a bit of storage before the processing
Htmlentities (Trim ($postValue), ent_quotes, "utf-8");
Three. Read the Display page
Use PHP's regular function to put the read <br> restore, and then output
<?php
$show = eregi_replace (' &lt;br&gt;|&lt;br/&gt; ', ' <br> ', $dataStr);
?>


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.