Home > Article > Backend Development > What are the functions for removing spaces in php?
php functions to remove spaces are: 1. Use the trim function to remove ordinary spaces on both sides of the string; 2. Use the str_replace function to remove spaces in the string; 3. Use the php strtr function to remove spaces.
Recommended: "PHP Video Tutorial"
Removing spaces from strings in php
1. Use the php function trim():
trim($str)
to remove ordinary spaces on both sides of the string;
2. Use the php function: str_replace():
str_replace(' ','',$str)
3. Use the php function: strtr():
strtr($str,array(' '=>''))
4. Use the encapsulation function you wrote:
function trimall($str)//删除空格 { $limit=array(" "," ","\t","\n","\r"); $rep=array("","","","",""); return str_replace($limit,$rep,$str); }
5. Use this regular expression to remove ordinary spaces:
preg_replace('# #', '', $str)
6. Use this regular expression to remove all spaces including full-width spaces:
$str =preg_replace("/\s| /","",$str);
The above is the detailed content of What are the functions for removing spaces in php?. For more information, please follow other related articles on the PHP Chinese website!