The function of the PHP trim () function and how to use it

Source: Internet
Author: User

The PHP trim () function is generally used to remove whitespace characters (or other characters) at the beginning and end of a string, and is generally used to process the received user data on the server side, so as not to store the user's input blank space in the database, the next time the data is compared.

The function has two parameters, the second one can be empty, and the format is as follows:

Trim (String $str [, string $character _mask = "\t\n\r\0\x0b"]);

The $STR is a string that needs to be processed, and if $character_mask is not empty, it will only clear $character_mask of the required characters written on it; if the second argument $character_mask is empty, the following characters are filtered:

  • "" (ASCII (0x20)), plain space.
  • "\ T" (ASCII 9 (0x09)), tab.
  • "\ n" (ASCII (0x0A)), line break.
  • "\ R" (ASCII (0x0D)), carriage return.
  • "\" (ASCII 0 (0x00)), null byte character.
  • "\x0b" (ASCII One (0x0B)), Vertical tab.

After data processing, the filtered string is returned.

Set a parameter $str:

<?php$data = "I have a space"; $data 2 = ";"; echo $data. $data 2;echo "<br>", echo trim ($data) $data 2;echo "<br>";

Trim function whitespace removal effect

Obviously, after the trim function is processed, the whitespace of the string data is removed.

We add a second parameter to see the effect:

<?php$data = "I have a space"; $data 2 = ";"; echo $data. $data 2;echo "<br>", echo trim ($data, ' \ t '). $data 2;//Specifies to delete the tab symbol \techo "<br>";

Trim Remove Tabs

Obviously, our data does not have a tab, so even if the trim is processed, our space is still there.

My blog Original: PHP trim () function and how to use

http://www.wangtuizhijia.com/archives/125

The function of the PHP trim () function and how to use it

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.