php如何去掉两边空格

第一种方法:通过php自带的函数

trim() 函数移除字符串两侧的空白字符或其他预定义字符。

<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>

第二种方法:通过正则表达式替换,功能更强
php去除字符串首尾空格(包括全角)

<?php
$str="     hello world     "; 
$str = mb_ereg_replace('^( | )+', '', $str); 
$str = mb_ereg_replace('( | )+$', '', $str); 
echo mb_ereg_replace('  ', "\\n  ", $str); 
?>
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容