How to remove spaces before and after a string in php: You can use the php built-in function trim() to achieve this. The trim() function is used to remove blank characters or other predefined characters on both sides of a string, for example: [trim($str,"Hed!")].
#trim() function removes whitespace characters or other predefined characters on both sides of a string.
(Recommended tutorial: php graphic tutorial)
Syntax:
trim(string,charlist)
Parameter introduction:
string Required. Specifies the string to check.
charlist Optional. Specifies which characters are removed from a string. If this parameter is omitted, all of the following characters are removed:
"\0" - NULL
"\t" - Tab
"\n" - Newline
"\x0B" - vertical tab
"\r" - carriage return
" " - space
(Video tutorial recommendation: php video tutorial)
Implementation code:
<?php $str = "Hello World!"; echo $str . "<br>"; echo trim($str,"Hed!"); ?>
Run result:
Hello World! llo Worl
The above is the detailed content of How to remove spaces before and after a string in php. For more information, please follow other related articles on the PHP Chinese website!