Home>Article>Backend Development> How to remove all spaces from string in php
php method to remove all spaces in a string: first trim can only remove spaces on both sides of the string; then expand the trim function, the code is [function trimall($str), $oldchar=array(" ", " ","\t","\n","\r")].
php method to remove all spaces in a string:
php removes all spaces in a string, actually Extension of the trim function, trim can only delete spaces on both sides of the string
The code is as follows:
function trimall($str)//删除空格 { $oldchar=array(" "," ","\t","\n","\r"); $newchar=array("","","","",""); return str_replace($oldchar,$newchar,$str); }
Test:
$str = " a b c d e f "; echo trimall($str);
Output result:
abcdef
Related learning recommendations:php programming(video)
The above is the detailed content of How to remove all spaces from string in php. For more information, please follow other related articles on the PHP Chinese website!