Home  >  Article  >  Backend Development  >  How to remove leading and trailing spaces from string in php

How to remove leading and trailing spaces from string in php

王林
王林Original
2020-08-06 14:56:452491browse

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)

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!

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