PHP trim() Function: Description, Syntax, Parameters, Return Value, and Examples
This article explains PHP's built-in trim() function, covering its purpose, related ltrim() and rtrim() functions, syntax, parameters, default character list, return value, and provides two practical code examples along with links to further learning resources.
The trim() function is a built-in PHP function that removes whitespace and predefined characters from both ends of a string.
Related Functions
ltrim() : removes characters from the left side of a string. rtrim() : removes characters from the right side of a string.
Syntax
<code>trim($string, $charlist)</code>Parameters
$string (required): the input string.
$charlist (optional): a list of characters to strip. If omitted, the function removes the following characters: \0 (NULL), \t (tab), \n (newline), \x0B (vertical tab), \r (carriage return), and ordinary whitespace.
Return value : the trimmed string.
Usage Examples
Example 1
<code><?php
$str = "Hello World!";
echo trim($str, "Hell!");
?></code>Output:
<code>o World</code>Example 2
<code><?php
header("content-type:text/html;charset=utf-8");
$str = " 欢迎来到php中文网! ";
echo trim($str);
?></code>Output:
<code>欢迎来到php中文网!</code>Learning Recommendations
Links to tutorials on Vue3+Laravel8+Uniapp, Vue3+TP6 API social e‑commerce development, Swoole, and Workerman+TP6 real‑time chat systems are provided.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.