The ultimate way to remove spaces and newline characters from a string in PHP ____php

Source: Internet
Author: User
Q-net > Article Tutorials > Programming design > PHP Delete a string of spaces and newline characters ultimate Method string newline character file encoding Web page encoding hidden characters the ultimate method of removing spaces and line breaks in a string in PHPAsk the net 2015-04-03 23:38:42 1640 Browse Article CatalogWhat is a space. What is a newline. HTML PHP Delete method to display hidden characters

Sometimes the code we generate can be more or less encountered in code or in strings with extra spaces, newline and other characters, but these characters in the browser is not displayed, any line and more space, only show a space, which is bound to the optimization of the Web page has this extremely bad impact, So today I'm going to share a few of the blanks and newline characters in the PHP deletion string. what is a space.

The space is a nothing character, more special, so when you fill out the form in the site, you can write nothing, but do not in the blank key to press the space bar, when you search the search engine, if you forget one of the words, can be temporarily replaced by space. what is a newline.

As the name suggests, another line of written text, in different languages and software have different ways of expression. HTML

The wrapping in HTML is <BR/> you can insert a simple line break, the label is an empty label (meaning that it has no end tag, so this is the wrong:<br></br>). In XHTML, the end tag is placed in the start tag, which is <br/>.

Note that the,<br> tag simply starts a new line, and when the browser encounters a <p> tag, it usually inserts some vertical spacing between adjacent paragraphs. PHP

There are several hidden characters in PHP, \ r \ n \ t, and we generally don't see these characters in the source code, but they are real, so let's say they all have those differences.

\ n Soft return :

Represents a newline and returns to the beginning of the next line in Windows. Equivalent to the effect of \ r in Mac OS.

In Linux, Unix only means wrapping, but does not go back to the beginning of the next line.

\ r Soft Space:

In Linux and UNIX, the representation is returned to the beginning of the line.

Represents a newline in Mac OS and returns to the beginning of the next line, equivalent to the \ n effect in Windows.

\ t jumps (moves to the next column).

They are valid in double quotes or in a string represented by a delimiter, and are not valid in a string of single quotes.

\ r \ n is commonly used to represent the return key on the keyboard, or only \ n.

\ t represents the "tab" key on the keyboard.

As you use Enter and Shift+enter, if the effect you want to display on the page is translated into HTML code or ...

newline symbol in file:

Linux,unix: \ r \ n
 windows: \ n
 Mac OS: \ r

corresponding: \ n/0x0a in LF or ASCII \ r 0x0d in CR or ASCII () \ t horizontal tab-HT or 0x09 (9) \ backslash \$ dollar \ "Double quote deletion method in ASCII"

There are a number of ways to remove such annoying hidden characters in PHP, see the following introduction.

1, using Str_replace to replace

$str = Str_replace (Array ("\ r \ n", "\ r", "\ n"), "", $str);

2, the use of regular replacement

$str = preg_replace ('//s*/', ', ', $str);

3. Use PHP to define a good variable (recommended)

$str = Str_replace (Php_eol, ', $str);

Of course we can define him as a function and replace it together so that we can reuse this code fragment.

function Trimall ($str) {
$qian =array ("", "", "\ T", "\ n", "\ R");
Return Str_replace ($qian, ', $str);
}
Show hidden characters

We do not know what this hidden character is, but we can use PHP nl2br into the foreground can be displayed in the line, and then the right remedy, you can easily resolve the line-wrapping and space string problems.

$str = "a
b
e
f
C";
echo nl2br ($STR);

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.