Home > Article > Backend Development > PHP removes spaces and trim is invalid, use str_replace to implement_PHP tutorial
PHP has the related function trime to remove spaces. It can remove two spaces or directly use the ltrim and rtrim functions. The results are the same as trim. If you want to delete all spaces, you can only use str_replace.
Usually, I use trim to filter out spaces. Today I found out that the spaces in the middle cannot be removed. Unfortunately, after checking the instructions, I found out that trim can only remove spaces at both ends, such as $abc = a b c;, and using trim can only remove spaces. The spaces before a and after c are removed, but what about the spaces before and after b. str_replace comes into play, and that’s it.
The code is as follows
|
Copy code
|
||||
str_replace(' ','',$abc).
| Students who need to remove spaces, please be careful. PHP removes spaces, trim does not work, str_replace does, but trim can delete the leading spaces as follows
The code is as follows
|
Copy code |