第一种是正则:php
<?php echo preg_replace('# #', '', 'ab ab'); //input "abab" ?>
第二种使用str_replace()函数:web
<?php echo str_replace(' ', '', 'ab ab'); //input "abab' ?>
第三种使用strtr()函数:函数
<?php echo strtr('ab ab', array(' '=>'')); // input "abab" ?>
strtr()函数使用上有点特别,实质上:.net
<?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'); ?>