Php program code for removing spaces from variables

Source: Internet
Author: User
There are several methods to clear spaces in variables, because trim cannot be deleted many times. below are several methods for you to refer. in PHP, we need to clear spaces in the variable. First, let's look at this method. the code is as follows: $ ar... there are several methods to clear spaces in variables, because trim cannot be deleted many times. below are several methods for you to refer.

In PHP, we need to clear spaces in the variable. First, let's look at this method. the code is as follows:

$arr = explode(" ",$a); foreach ($arr as $value) {     $result.=$value; }


Echo "after the blank is removed:". $ result; there is another method, the code is as follows:

$ Str = str_replace ("", "", $ str); $ str = str_replace (chr (32), "", $ str)

The preceding method can only process English spaces. the following method can process only Chinese spaces. However, if encoding is specified on the page, garbled characters may occur. the code is as follows:

$ Str = str_replace (chr (32), "", $ str); $ str = str_replace (chr (161), "", $ str );

For the page for UTF-8 is, after the Chinese space is submitted into the chr (227), the original should be chr (161), followed by the test using trim (), ltrim (), rtrim () functions can also achieve our desired results.

The syntax and usage of trim () system functions are described as follows:

String trim (string $ str [, string $ charlist])

The trim () function removes spaces between the start and end positions of the input string. In fact, it is not just a space. if the second parameter is not set, the trim () function removes the following characters:

"": Space;

"\ T": tab;

"\ N": Line break;

"\ R": carriage return;

"\ 0": null character;

"\ X0B": Vertical Tab;

If the second parameter is set, the trim () function removes only the character set in the second parameter and does not remove the default character set. the code is as follows:

 $str_trim";

// Check the output:

hello world hello world

How is the same? Why does the space in the first line disappear? let's take a look at the HTML code of the webpage:

Hello world

The space is displayed. the space is not displayed because the browser automatically displays spaces without division. the code is as follows:

 $str_trim";

// The output result is:

Left hello world Right hello world Right

We will remove all the characters related to the eftL on the left. you may have noticed that the "t" in the Right on the Right is also in the second parameter. why is it not removed, take a closer look at $ str. The last character is actually a space. because the space (rightmost) is not removed (can be seen in HTML), "t" (second to bottom on the right) the second parameter of the function overwrites the default character set of trim.

Finally, let's talk about the ltrim () and rtrim () functions:

The ltrim () function only processes the left side of the string, and the rtrim () function only processes the right side of the string.

Regular expression replacement is more powerful. php removes spaces at the beginning and end of the string (including the full angle). The code is as follows:

 

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.