Home >Backend Development >PHP Problem >How to remove leading and trailing spaces from string in php
php method to remove leading and trailing spaces from a string: You can use PHP's built-in function trim() to remove leading and trailing spaces from a string. The trim() function removes whitespace characters or other predefined characters on both sides of a string and returns the modified string.
Thetrim() function removes whitespace characters or other predefined characters from both sides of a string and returns the modified string.
(Recommended tutorial: php graphic tutorial)
Syntax:
trim(string,charlist)
Parameters:
string Required. Specifies the string to check.
#charlist Optional. Specifies which characters are removed from the string.
(Video tutorial recommendation: Introduction to Programming)
Code implementation:
<?php $str = " Hello World! "; echo "Without trim: " . $str; echo "<br>"; echo "With trim: " . trim($str); ?>
Output result:
ithout trim: Hello World! <br>With trim: Hello World!
The above is the detailed content of How to remove leading and trailing spaces from string in php. For more information, please follow other related articles on the PHP Chinese website!