- PHP trim
- Introduction to the PHP trim() function
- PHP trim() function examples
- 1) Using the PHP trim() function to remove whitespace from both ends of a string
- 2) Using the PHP trim() function to remove other characters
- Summary
- PHP trim() Function
- Definition and Usage
- Syntax
- Parameter Values
- Technical Details
- More Examples
- Example
- Example
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- trim
- Возвращаемые значения
- Примеры
- Примечания
- Смотрите также
- trim
- Возвращаемые значения
- Примеры
- Примечания
- Смотрите также
- User Contributed Notes 2 notes
PHP trim
Summary: in this tutorial, you’ll learn how to use the PHP trim() function to remove whitespace or other characters from both ends of a string.
Introduction to the PHP trim() function
The trim() function removes the whitespaces or other characters from the beginning and end of a string.
Here’s the syntax of the trim() function:
trim ( string $string , string $characters = " \n\r\t\v\0" ) : string
Code language: PHP (php)
The trim() function has two parameters:
- $string is the input string that will be trimmed.
- $characters argument is optional. It specifies which character to remove from both ends of the $string .
By default, the trim() function removes the following characters:
Character | ASCII | Hex | Description |
---|---|---|---|
” “ | 32 | 0x20 | a space |
“\t” | 9 | 0x09 | a tab |
“\n” | 10 | 0x0A | a new line |
“\r” | 13 | 0x0D | a return |
“\0” | 0 | 0x00 | a NUL-byte |
“\v” | 11 | 0x0B | a vertical tab |
If you want to remove other characters, you can specify them in the $characters argument.
To specify a range of characters to remove, you can use the ‘..’. For example, to remove all ASCII control characters from both ends of a string, you use the following value for the $characters argument:
'\x00..\x1F'
Code language: PHP (php)
It’s important to note that the trim() function doesn’t change the input string. It returns a new string with all the $characters removed.
PHP trim() function examples
Let’s take some examples of using the PHP trim() function.
1) Using the PHP trim() function to remove whitespace from both ends of a string
The following example uses the trim() function to remove spaces from the beginning and the end of a string:
$str = ' PHP '; $new_str = trim($str); var_dump($new_str);
Code language: PHP (php)
string(3) "PHP"
Code language: plaintext (plaintext)
2) Using the PHP trim() function to remove other characters
Suppose you have the following URL:
https://www.phptutorial.net/api/v1/posts/
Code language: PHP (php)
To get the request URI, you access the $_SERVER array with the key ‘REQUEST_URI’ :
$uri = $_SERVER['REQUEST_URI']; echo $uri;
Code language: PHP (php)
/api/v1/posts/
Code language: plaintext (plaintext)
The URI contains both leading and trailing slashes.
To remove the slashes ( / ) from both ends of the URI, you can use the trim() function:
$uri = $_SERVER['REQUEST_URI']; echo trim($uri,'/');
Code language: PHP (php)
api/v1/posts
Code language: plaintext (plaintext)
Summary
- Use the PHP trim() function to remove whitespaces or other characters from both ends of a string.
PHP trim() Function
Remove characters from both sides of a string («He» in «Hello» and «d!» in «World»):
Definition and Usage
The trim() function removes whitespace and other predefined characters from both sides of a string.
- ltrim() — Removes whitespace or other predefined characters from the left side of a string
- rtrim() — Removes whitespace or other predefined characters from the right side of a string
Syntax
Parameter Values
- «\0» — NULL
- «\t» — tab
- «\n» — new line
- «\x0B» — vertical tab
- «\r» — carriage return
- » » — ordinary white space
Technical Details
Return Value: | Returns the modified string |
---|---|
PHP Version: | 4+ |
Changelog: | The charlist parameter was added in PHP 4.1 |
More Examples
Example
Remove whitespaces from both sides of a string:
The HTML output of the code above will be (View Source):
Without trim: Hello World!
With trim: Hello World!