php method to remove spaces in the middle: 1. Use regular expressions to remove spaces in the middle of a string; 2. Use str_replace() function to remove; 3. Use strtr() function to remove; 4. Use encapsulation function to remove spaces in the middle. .
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php deletes the spaces in the middle of the string
First method: Use regular expressions
The code is as follows:
<?php echo preg_replace('# #', '', 'ab ab'); //输出 "abab" ?>
Second method: Use str_replace() function
The code is as follows:
<?php echo str_replace(' ', '', 'ab ab'); //输出 "abab' ?>
Third method: use strtr() function
The code is as follows:
<?php echo strtr('ab ab', array(' '=>'')); // 输出 "abab" ?>
<?php strtr('ewb', 'web', '123') == strtr('ewb', array('e '=> '2', 'w' => '1', 'b' => '3')) == str_replace(array('e', 'w', 'b'), array('2', '1', '3'), 'ewb'); ?>
Fourth method: use the encapsulated function
function trimall($str)//删除空格 { $qian=array(" "," ","\t","\n","\r"); $hou=array("","","","",""); return str_replace($qian,$hou,$str); }
PHP Video Tutorial"
The above is the detailed content of How to remove middle spaces in php. For more information, please follow other related articles on the PHP Chinese website!