Home >Backend Development >PHP Problem >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

WJ
WJOriginal
2020-06-02 13:09:382971browse

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!

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