How to remove leading and trailing spaces from a string in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:18:48
Original
903 people have browsed it

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);
?>

www.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. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template