Php program code for removing spaces from variables

Source: Internet
Author: User

In PHP, we need to clear spaces in the variable. First, let's look at the following method:

The code is as follows: Copy code

$ Arr = explode ("", $ );
Foreach ($ arr as $ value)
{
$ Result. = $ value;
}
Echo "removed blank:". $ result;

Another method is as follows:

The code is as follows: Copy code

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

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

The code is as follows: Copy code

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

For the UTF-8 for the page is, after the Chinese space is submitted into the chr (227), the original should be chr (161)

The following tests show that using the 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 (not just spaces) between the start position and the end position of the input string ).
If the second parameter is not set, the trim () function removes the following characters:
"": Space;
"\ T": tab;
"\ N": Line break;
"\ R": Carriage return;
"\ 0 & Prime;: 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 above.

The code is as follows: Copy code
1 <? Php
2 $ str = "hello world ";
3 $ str_trim = trim ($ str );
4 echo "$ str <br/> $ str_trim ";
5?>
Let's take a look at the output:
Hello world

How is the same? Why does the space in the first line disappear. Haha. 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: Copy code
1 <? Php
2 $ str = "Left hello world Right ";
3 $ str_trim = trim ($ str, "eftL ");
4 echo "$ str <br/> $ str_trim ";
5?>
Output result:
Left 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. The last character of $ str is actually a space. Because Spaces (rightmost) are not removed (can be seen in HTML), "t" (penultimate on the right) will not be processed. This also indicates that the second parameter of the function will overwrite 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;
The rtrim () function only processes the right side of the string;

Use regular expressions to replace and improve functions

Php removes spaces at the beginning and end of the string (including the full angle)

The code is as follows: Copy code

<?
$ Str = "www.www.www.111cn.net ";
$ Str = mb_ereg_replace ('^ (|) +', '', $ str );
$ Str = mb_ereg_replace ('(|) + $', '', $ str );
Echo mb_ereg_replace (''," \ n ", $ 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.