Home > Article > Backend Development > How to remove spaces at the beginning and end of a string in php
How to remove spaces at the beginning and end of a string in php?
You can use the trim() function
Definition and usage:
The trim() function removes whitespace characters or other predefined characters at both ends of a string.
Syntax:
trim(string,charlist)
string:必需。规定要检查的字符串。 charlist:可选。规定从字符串中删除哪些字符。
<?php $str = " Hello World! "; echo "Without trim: " . $str; echo "<br>"; echo "With trim: " . trim($str); ?>
The above code output:
Without trim: Hello World!
With trim: Hello World!
Related references:php中文网
The above is the detailed content of How to remove spaces at the beginning and end of a string in php. For more information, please follow other related articles on the PHP Chinese website!