PHP implementation of HTML source code in the carriage return line output

Source: Internet
Author: User

Knowledge expansion

Transfer sequence Description
N Line Wrap
R Enter
T Horizontal tab
[/td> Back slash
$ Dollar sign
" Double quotes
[0-7] {1,3} This regular expression sequence matches a character represented by the octal symbol
x[0-9a-fa-f]{1,2} This regular expression sequence matches a character that is represented by a hexadecimal symbol

Using double quotes (") to define a string, PHP knows more about the escape sequence for special characters:


PHP default page encoding is HTML, for the output of the content to wrap, you must use the HTML Line wrap label "<br>" or "<br/>"
, when using PHP to do the HTTP API, processing the BR tag is a bit cumbersome, you can specify the PHP page encoding, so that "\ n" successfully resolved to wrap.

The code is as follows Copy Code

<?php
Header ("Content-type:text/txt; charset=gb2312 ");
for ($i =0; $i < $i + +) {
echo "string";
echo "\ n";
?>

With a browser, 100 line "string" is displayed directly.
You can also declare the page encoding as

The code is as follows Copy Code


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

In this case, access to the browser, will automatically download as a document, with code to download no difference.

TXT file line breaks do not work when using fwrite newline write

We all know the line break in PHP: \ n, carriage return: \ r, when you need to change lines, it's usually a combination of "\ r \ n". But why does the newline character not work when we write a file using fwrite? Let's look at the following example:

The code is as follows Copy Code

<?php
$filename = ' file.txt ';
$word = ' Hello! \ r \ n Welcome to Www.111cn.net ';
$fh = fopen ($filename, "a"); W writes a append write from the beginning
Echo fwrite ($fh, $word);
Fclose ($FH);
?>

A carriage return line character "\ r \ n" is added to the $word string, but the output is not expected, and the carriage return line character "\ r \ n" is not resolved to be a newline character, but is directly treated as a char output.

Why is there such a situation? After study, the original is a single double quotation mark to cause trouble! We put the single quotation mark "'" of the $word definition string into double quotation marks "". The correct wording is as follows:

The code is as follows Copy Code

<?php
$filename = ' file.txt ';
$word = "Hello!" \ r \ n Welcome to Www.111cn.net ";
$fh = fopen ($filename, "a"); W writes a append write from the beginning
Echo fwrite ($fh, $word);
Fclose ($FH);
?>

In the above 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.