The trim function that comes with php can only replace the spaces at the left and right ends, it feels like In some cases, it doesn't work very well. If we want to filter out all whitespace characters in a string (spaces, double-width spaces, newlines, 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: jkgsdgsgsdgsgsdggsd, which perfectly achieves the effect we want.
The above is how PHP filters all whitespace characters. I hope it will be helpful to everyone's learning.