How to remove spaces at the beginning and end of a string in php

WJ
Release: 2023-03-01 11:34:01
Original
2918 people have browsed it

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)
Copy after login
string:必需。规定要检查的字符串。
charlist:可选。规定从字符串中删除哪些字符。
Copy after login
<?php
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
Copy after login

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!

Related labels:
php
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