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

How to remove spaces from string in PHP

藏色散人
藏色散人Original
2018-11-30 17:57:075883browse

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

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

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!

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