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

Source: Internet
Author: User
Tags regular expression

In the source code to enter a newline but do not display such as BR characters we need to escape the characters in PHP to achieve, the following is a look at it.

   

Knowledge expansion

[/td> >
transition sequence description
n line wrap
r return
horizontal tab
backslash
$ $ sign
" double quotes
[0-7]{1,3}
x[0-9a-fa-f]{1,2} This regular expression sequence matches a character represented in hexadecimal notation

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

<?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

<?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

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.