How to remove leading and trailing spaces from string in php

王林
Release: 2023-03-03 22:32:02
Original
2346 people have browsed it

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.

How to remove leading and trailing spaces from string in php

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)
Copy after login

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:

";
echo "With trim: " . trim($str);
?>
Copy after login

Output result:

ithout trim: Hello World! 
With trim: Hello World!
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!