Home>Article>Backend Development> How to remove spaces in string in php
php method to delete spaces in the middle of a string
Method 1: Use regular expressions
The code is as follows:
Method 2: Use str_replace() function
The code is as follows:
Method 3: Use strtr() function
The code is as follows:
'')); // 输出 "abab" ?>
strtr() function is a bit special in use. In essence:
The code is as follows:
'2', 'w' => '1', 'b' => '3')) == str_replace(array('e', 'w', 'b'), array('2', '1', '3'), 'ewb'); ?>
Method 4: Using the encapsulated function
The code is as follows:
function trimall($str)//删除空格 { $qian=array(" "," ","\t","\n","\r"); $hou=array("","","","",""); return str_replace($qian,$hou,$str); }
Recommended tutorial: "PHP Tutorial"
The above is the detailed content of How to remove spaces in string in php. For more information, please follow other related articles on the PHP Chinese website!