In this PHP tutorial, you shall learn how to trim spaces from edges of given string using trim() function, with example programs.

PHP – Trim spaces from edges of string

To trim spaces from edges of a string in PHP, we can use trim() function.

Call trim() function and pass the given string as argument. The function returns a new string created by trimming off the spaces from edges of the original string.


The syntax to trim the spaces around string str using trim()function is



1. Remove spaces from edges of string

In this example, we take a string str and remove the spaces from its leading and trialing edges.

PHP Program

  $str = '    apple      ';
  $output = trim($str);
  echo $output;



In this PHP Tutorial, we learned how to delete the spaces at leading and trialing edges of given string, using trim() function.