Test Page code:
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Use PHP to obtain line breaks in form area data </title>
</Head>
<Body>
<? Php
$ Content = empty ($ _ POST ['content'])? Null: trim ($ _ POST ['content']);
If (! Empty ($ content) echo str_replace ("\ r", 'rl ', nl2br ($ content ));
Echo "\ r". '<br/> ---------- split line ----------------------'. "\ r ";
If (! Empty ($ content) echo str_replace ("\ n", 'nl ', nl2br ($ content ));
Echo "\ n". '<br/> ---------- split line ----------------------'. "\ n ";
If (! Empty ($ content) echo str_replace ("\ r", 'rl ', str_replace ("\ n", 'nl', nl2br ($ content )));
Echo "\ r". '<br/> ---------- split line -------------------- <br/>'. "\ n ";
Echo 'Hello'. "\ n". 'Boys! ';
Echo 'Hello'. "\ r". 'Boys! ';
?>
<Form action = "textareanl. php" method = "post" enctype = "multipart/form-data">
<Textarea name = "content" cols = "20" rows = "6"> </textarea>
<Br/>
<Input type = "submit" value = "submit"/>
</Form>
</Body>
</Html>
Open it in the browser and enter the following in the form:
After you press submit, the browser displays the following results:
In notepad Coco, you can see the following results:
The preceding results show that:
1. the PHP function nl2br () inserts an HTML line break before each new line (\ r \ n) in the string: <br/>;
2. In Windows, the line feed is (\ r \ n );
3. In notepad, \ r or \ n have the line feed function;