Home >Backend Development >PHP Problem >What are the ways to remove spaces in strings in php

What are the ways to remove spaces in strings in php

王林
王林Original
2021-09-18 15:19:144380browse

php methods to remove spaces in strings are: 1. Regular expressions, such as [preg_replace('# #', '', 'ab ab');]; 2. str_replace() function; 3. strtr() function.

What are the ways to remove spaces in strings in php

The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.

In PHP we have many ways to clear spaces, such as regular expressions, str_replace() function, strtr() function, etc. When we use these methods, we will find that removing spaces from strings is a very simple matter.

Let’s take a look at how these methods remove spaces from strings.

The first one is regular:

What are the ways to remove spaces in strings in php

The second one uses str_replace() function:

What are the ways to remove spaces in strings in php

The third way to use the strtr() function:

What are the ways to remove spaces in strings in php

The strtr() function is a bit special to use, essentially:

What are the ways to remove spaces in strings in php

Recommended learning: php training

The above is the detailed content of What are the ways to remove spaces in strings 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