Home > Backend Development > PHP Tutorial > How to remove spaces from string in PHP

How to remove spaces from string in PHP

藏色散人
Release: 2023-04-04 13:48:02
Original
5944 people have browsed it

PHP removes spaces in the string. We can use the PHP trim() function to achieve this.

We can use the PHP trim() function to remove spaces containing non-breaking spaces, newlines, and tabs from the beginning and end of a string. But it does not remove the spaces appearing in the middle of the string.

The code picture is as follows:

How to remove spaces from string in PHP

The output result is:

3328
Copy after login

You can also copy the following code and test it locally:

<?php
$my_str = &#39;    Welcome to Tutorial Republic &#39;;
echo strlen($my_str); // Outputs: 33

$trimmed_str = trim($my_str);
echo strlen($trimmed_str); // Outputs: 28
Copy after login

This article is a brief introduction to removing spaces in strings in PHP.

The above is the detailed content of How to remove 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