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.

php Courses
php Courses
php Courses
PHP trim() Function: Description, Syntax, Parameters, Return Value, and Examples

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

trim($string, $charlist)

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

<?php
$str = "Hello World!";
echo trim($str, "Hell!");
?>

Output:

o World

Example 2

<?php
header("content-type:text/html;charset=utf-8");
$str = "  欢迎来到php中文网!  ";
echo trim($str);
?>

Output:

欢迎来到php中文网!

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPStringfunction.trimexample
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.