PHP implements line feed output for carriage return in html source code

Source: Internet
Author: User

PHP implements line feed output for carriage return in html source code

Enter a line break in the source code, but do not display characters such as br. We need to use php to turn the characters into characters. Let's take a look at the following.

 

 

Knowledge Expansion

 

Transfer sequence Description
N Line feed
R Enter
T Horizontal Tab
[/Td> Backslash
$ Dollar sign
" Double quotation marks
[0-7] {1, 3} This regular expression matches a character in the octal symbol.
X [0-9A-Fa-f] {1, 2} This regular expression matches a character in the hexadecimal notation.

 

Use double quotation marks (") to define strings. PHP understands the escape sequences of more special characters:


PHP uses HTML by default. to wrap the output content, you must use the HTML line feed label "<br>" or "<br/>"
When using PHP for http api, it is a little troublesome to process the br tag. In this case, you can specify the PHP page encoding so that "\ n" is successfully resolved to a line break.

The Code is as follows: Copy code

<? Php
Header ("Content-type: text/txt; charset = gb2312 ");
For ($ I = 0; I I <100; $ I ++ ){
Echo "string ";
Echo "\ n ";
?>

When accessed using a browser, 100 rows of "string" are displayed ".
You can also declare the Page code

The Code is as follows: Copy code


Header ("Content-Type: plain/text; charset = gb2312 ");

When accessed in a browser, the file is automatically downloaded. There is no difference in downloading code.

Line breaks do not work when the txt file is written using fwrite line breaks.

We all know the linefeed of php: \ n, carriage return: \ r. When we need to change the line, we usually use the "\ r \ n" combination. But why \ n linefeed does not work when we use fwrite to write a file. Let's take a look at the following example:

The Code is as follows:  

<? Php
$ Filename = 'file.txt ';
$ Word = 'Hello! \ R \ n welcome to www.111cn.net ';
$ Fh = fopen ($ filename, "a"); // w writes an appended write from the beginning to
Echo fwrite ($ fh, $ word );
Fclose ($ fh );
?>

$ Word is added with the carriage return linefeed "\ r \ n", but the output is not as expected. The carriage return linefeed "\ r \ n" is not parsed as a linefeed, it is directly output as a character.

Why is this happening? After research, it turns out to be a single double quotation mark! We can replace the single quotation mark "'" of the $ word string with the double quotation mark. The correct syntax is as follows:

The Code is as follows:  

<? Php
$ Filename = 'file.txt ';
$ Word = "Hello! \ R \ n welcome to www.111cn.net ";
$ Fh = fopen ($ filename, "a"); // w writes an appended write from the beginning to
Echo fwrite ($ fh, $ word );
Fclose ($ fh );
?>

In the preceding example, echo fwrite () shows a number that represents the length of the string.

Related Article

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.