In php, there are three methods to delete spaces in the middle of a string. First, use the regular expression to copy the code as follows :? Phpechopreg_replace (##,, abab); output abab? Type 2: Use the str_replace () function to copy the code as follows :?
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 );
}
The pipeline code is as follows :? Php echo preg_replace ('#', '', 'AB AB'); // output" abab "? Type 2: Use the str_replace () function code as follows :?...