Home  >  Article  >  Backend Development  >  How to remove spaces before and after a string in php

How to remove spaces before and after a string in php

王林
王林Original
2020-08-19 13:51:313480browse

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!")].

How to remove spaces before and after a string in php

#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:

";
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn