[PHP source Reading]trim, RTrim, LTrim functions

Source: Internet
Author: User
Tags php source code rtrim

The Trim series function is used to remove whitespace or other characters from the end of a string. The LTrim function only removes characters from the string header, and the RTrim function only strips the characters at the end of the string.

Trim

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

Parameter description

Character_mask
The default is white space characters such as "\t\n\r\0\x0b".

Use.. You can specify a range of characters. Note here, "..." The left and right sides are a pair of valid range values, and if passed an illegal value will be an error.

Run the sample

Let's take a look at the normal use:

$str = ' Hello. ' ; $new _str Trim ($str, '. ') ); The result is Hello

A more bizarre result. The error here is because PHP put. The left and right sides are considered to be range values, and here ' ... ' On the left is the character '. ', which inside PHP will be considered to be a missing right boundary range value.

$str = ' Hello ... ';
$second _strtrim($str// error

The second parameter uses a valid boundary value:

$str = ' HELLOABCDEFG '; $new _str Trim ($str, ' A. G '); Output Hello

Trim execution Steps

Trim, LTrim, RTrim three functions all call the Php_do_trim function, the difference is that the second parameter mode is different. This paper mainly analyzes the trim function, and the LTrim and RTrim functions are similar to trim. The Php_do_trim then calls the Php_trim to implement the function, so the core function of the TRIM function is Php_trim function. The following steps are performed:

1. Set the mask array to save the filter characters according to the value of what

2. Filter the character to be filtered at the string header

3. Filter the characters to be filtered at the end of the string

The Php_trim function performs the following flowchart:

SOURCE Interpretation

The Php_trim function calls Php_charmask first, and the function tries to set the filter character to Mask[char] = 1, which is a hash array and can then be used for subsequent judgments. If the second parameter is a range value, the Memset function is called to assign a value to the mask array.

When deciding which filter to use with the mode variable, there is a small optimization, which is used internally in PHP instead of multiple judging conditions. The code for this section is as follows:

    if(Mode &1) {         for(i =0; i < Len; i++) {            if(mask[(unsignedChar) C[i]) {trimmed++; } Else {                 Break; }} Len-=trimmed; C+=trimmed; }    if(Mode &2) {         for(i = len-1; I >=0; i--) {            if(mask[(unsignedChar) (C[i]]) {len--; } Else {                 Break; }        }    }

The process of judging:

1 && 1 = = 1 Left to filter
2 && 1 = = 0 The left side does not need filtering
3 && 1 = = 1 Left to filter
1 && 2 = = 0 Right do not need to filter
2 && 2 = = 1 Right filter required
3 && 2 = = 1 Right filter required


Using bit operations like this can improve the efficiency of the program, and the code is more concise and readable.

Summary

Read the source of this function, first of all, in the C language, if you need to do a key value array, and the key value is a single character, you can use the type of unsigned char to do array subscript, so you can construct similar characters as subscript of the map array.

The second is to use bit arithmetic to improve program efficiency and code readability.

Original article, writing Limited, Caishuxueqian, if there is not in the text, million hope to inform.

If this article is helpful to you, please click on the recommendation, thank you ^_^

Finally, I have a more detailed comment on the PHP source code on GitHub. Interested can be onlookers, to a star. PHP5.4 source annotation.

More PHP source articles:

[PHP source reading]strlen function

"Performance for the King" from the PHP source analysis Array_keys and Array_unique

[PHP source Reading]strpos, strstr and Stripos, stristr functions

"PHP source reading" Explode and implode functions

[PHP source Reading]trim, RTrim, LTrim functions

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.