Home > Article > Backend Development > How to filter all whitespace characters in php
This article mainly introduces the method of filtering all whitespace characters in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
The trim function that comes with PHP can only replace the spaces at the left and right ends. I feel that it is not very easy to use in some cases. If you want to filter out all the whitespace characters in a string (spaces, full-width spaces, line break, etc.), then we can write a filter function ourselves.
Everyone who has learned the str_replace function in php knows that it can be replaced in batches, so we can use the following source code to replace and filter all whitespace characters in a string.
<?php $str = 'jkgsd gsgsdgs gsdg gsd'; echo myTrim($str); function myTrim($str) { $search = array(" "," ","\n","\r","\t"); $replace = array("","","","",""); return str_replace($search, $replace, $str); } ?>
Run the code and the page output is: jkgsdgsgsdgsgsdggsd, which perfectly achieves the effect we want.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
Definition and usage of the htmlentities() function in php
How to generate a verification code that can be clicked to refresh in PHP And a simple example
The above is the detailed content of How to filter all whitespace characters in php. For more information, please follow other related articles on the PHP Chinese website!