This article mainly introduces three methods in php to delete spaces in the string. For more information, see
First: use regular expressions
The Code is as follows:
Echo preg_replace ('#', '', 'AB ');
// Output "abab"
?>
Method 2: Use the str_replace () function
The Code is as follows:
Echo str_replace ('','', 'AB AB ');
// Output "abab'
?>
Third: Use the strtr () function
The Code is as follows:
Echo strtr ('AB AB', array (''=> ''));
// Output "abab"
?>
Strtr () functions are used in a special way, essentially:
The Code is as follows:
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
The Code is as follows:
Function trimall ($ str) // Delete Spaces
{
$ Qian = array ("", "", "\ t", "\ n", "\ r ");
$ Hou = array ("","","","","");
Return str_replace ($ qian, $ hou, $ str );
}