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

How to remove spaces before and after a string in php

王林
Release: 2023-03-04 19:06:01
Original
3751 people have browsed it

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

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:

<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
Copy after login

Run result:

Hello World!
llo Worl
Copy after login

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!

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