Trim in PHP is a built-in PHP function that eliminates white spaces as well as pre-defined characters from both the left and right sides of a string.

Đang xem: Php trim user input in the text field to avoid user just entering spaces

Syntax

trim(string,charlist)

As demonstrated in the above syntax and discussed below, the function accepts one obligatory parameter and one optional parameter.

$string: This parameter defines the string from which the left and right white space and pre-defined characters will be deleted.$charlist: This is an optional parameter that defines which characters from the string should be deleted. It eliminates all the following characters if this is not specified. 

Return Value: It returns a string that was edited by deleting white space and pre-defined characters on both sides.

As a result, if you do not specify the second parameter, the Trim in PHP function trims the following parameters by default:

The character ” ” (ASCII 32 (0x20) is a standard space characterThe tab character from Trim in PHP is “t” (ASCII 9 (0x09), and the new line character is “n” (ASCII 10 (0x0A)The carriage return character is “r” (ASCII 13 (0x0D)“0” (ASCII 0 (0x00) represents the NULL byte)The character “x0B” (ASCII 11 (0x0B) signifies a vertical tab

Stand Out From Your Peers this Appraisal SeasonStart Learning With Our FREE CoursesEnroll Now

*

Example: 

Output

*

Example

Output

*

What Are the Three Different Types of Trim Functions?

Trim() removes characters from the beginning and end of a string, thus when you remove characters from the center, it can be misleading. trim('abc', 'bad') eliminates both 'a' and 'b' by trimming 'a' and then relocates 'b' to the beginning to be trimmed as well. As a result, trim ('abc', 'b') appears to “work” whereas trim ('abc', 'b') appears to “fail.” Trim in PHP comes in three flavors, you can use each of which for the purposes listed below:

trim(): This is a standard function that is used to remove characters from the beginning and end of a string.ltrim() is a function that trims characters from the start of an input string.rtrim() is a function that trims characters off the end of a string.

Example

Output

*

This example demonstrates the results without using the Trim in PHP function in the previous example. It displays newlines as part of the sentence in the string assigned for testing purposes. In the output, you can see the same thing.

Xem thêm: Ngôn Ngữ Html Là Gì Và Tại Sao Nó Quan Trọng Với Website? Vai Trò Của Html Trong Lập Trình Web

To Remove a Character

Output

*

In the preceding example, you use the trim function to delete the tab (“t”) character by specifically stating the character in the trim function's second parameter, as shown. First, you must assign the required string to the str1 argument, and then use the $str new parameter to append a tab to the end of the $str1. Simultaneously, you invoke the trim function, removing the tab character from the string's end.

*

When you remove the trim method from the code, you can see that an extra tab is written at the end of the string because it was appended in a $str new parameter. As a result, the Trim in PHP functionality has been confirmed.

To Remove a Blank Space

Example

Output

*

The input string in the preceding example has a lot of blank spaces at the beginning. As a result, you’re going to use the trim function to get rid of it. As can be seen in the result, all of the blank spaces from the beginning have been removed, leaving only the correct input string. Please keep in mind that only the whitespace between the start and finish of the string is cut, not the ones in the middle of the sentence. The spaces between “example” and “to” are still the same.

Xem thêm:

Example

Output

*

Advance your career as a MEAN stack developer with the Full Stack Web Developer – MEAN Stack Master's Program. Enroll now!

Conclusion 

In this article, you went through the trim(), ltrim(), and rtrim() functions in detail, which are used to eliminate characters from a string that aren't required. You also learned how to trim using your whitespace character. If you want to learn more about the Trim in PHP concept, then you must enroll in this darkedeneurope.com's Full Stack Development course. Also if you are looking for a professional degree in the field then you must look for our skill up course and enjoy the best knowledge. 

Have any questions for us? Leave them in the comments section of this article. Our experts will get back to you on the same, ASAP.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *