Home >Backend Development >PHP Problem >What are the ways to remove spaces in strings in php
php methods to remove spaces in strings are: 1. Regular expressions, such as [preg_replace('# #', '', 'ab ab');]; 2. str_replace() function; 3. strtr() function.
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:
The second one uses str_replace() function:
The third way to use the strtr() function:
The strtr() function is a bit special to use, essentially:
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!