This article mainly introduces three methods in php to delete spaces in the string. For more information about how to delete spaces in the string, see the following article, for more information, see
First: use regular expressions
Echo preg_replace ('#', '', 'AB ');
// Output "abab"
?>
Method 2: Use the str_replace () function
Echo str_replace ('','', 'AB AB ');
// Output "abab'
?>
Third: use the strtr () function
Echo strtr ('AB AB', array (''=> ''));
// Output "abab"
?>
Strtr () functions are used in a special way, essentially:
Strtr ('ewb ', 'web', '20140901') =
Strtr ('ewb ', array ('E' => '2', 'W' => '1',' B '=> '3') =
Str_replace (array ('e', 'W', 'B'), array ('2', '1', '3'), 'ewb ');
?>
Type 4: Use encapsulated functions
Function trimall ($ str) // delete spaces
{
$ Qian = array ("", "", "\ t", "\ n", "\ r ");
$ Hou = array ("","","","","");
Return str_replace ($ qian, $ hou, $ str );
}