The first method: Use PHP’s built-in function
/*
trim to remove spaces at both ends of a string,
rtrim is to remove spaces from the right side of a string,
ltrim removes spaces from the left of a string.
*/
?>
echo trim("space")."
";
echo rtrim("space"). "
";
echo ltrim(" space")."
";
?>
The second method: replace by regular expression, more powerful
php removes leading and trailing spaces from strings (including full-width spaces)
Copy code The code is as follows:
$ str=" Script Home www.jb51.net ";
$str = mb_ereg_replace('^( | )+', '', $str);
$str = mb_ereg_replace('( | )+$ ', '', $str);
echo mb_ereg_replace(' ', "n ", $str);
?>
http://www.bkjia.com/PHPjc/325449.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325449.htmlTechArticleThe first method: use php’s own function?php /* trim to remove spaces at both ends of a string, rtrim removes spaces from the right side of a string, and ltrim removes spaces from the left side of a string. ...