Home >Backend Development >PHP Problem >What are the ways to remove spaces in strings in php
php methods to remove spaces in strings are: 1. Use regular expressions to remove spaces, such as [preg_replace('# #', '', 'ab ab')]; 2. Use the str_replace function Remove spaces; 3. Use the wrapper function to remove spaces.
The method is as follows:
(Learning video recommendation: java course)
1. Regular Expression
<?php echo preg_replace('# #', '', 'ab ab'); //输出 "abab" ?>
2. Use str_replace function
<?php echo str_replace(' ', '', 'ab ab'); //输出 "abab' ?>
3. Use encapsulation function
function trimall($str)//删除空格 { $qian=array(" "," ","\t","\n","\r"); $hou=array("","","","",""); return str_replace($qian,$hou,$str); }
Related recommendations:php training
The above is the detailed content of What are the ways to remove spaces in strings in php. For more information, please follow other related articles on the PHP Chinese website!