Home  >  Article  >  Backend Development  >  What function does php use to remove leading and trailing spaces?

What function does php use to remove leading and trailing spaces?

王林
王林Original
2019-09-24 17:54:273874browse

What function does php use to remove leading and trailing spaces?

The first method: through the function that comes with php

<?php
/*
trim 去除一个字符串两端空格,
rtrim 是去除一个字符串右部空格,
ltrim 是去除一个字符串左部空格。

*/
?>
<?php
echo trim(" 空格 ")."<br>";
echo rtrim(" 空格 ")."<br>";
echo ltrim(" 空格 ")."<br>";
?>

The second method: through regular expression Formula replacement, more powerful

php removes spaces at the beginning and end of the string (including full-width)

<?
$str="  php中文网 ";
$str = mb_ereg_replace(&#39;^( | )+&#39;, &#39;&#39;, $str);
$str = mb_ereg_replace(&#39;( | )+$&#39;, &#39;&#39;, $str);
echo mb_ereg_replace(&#39;  &#39;, "\n  ", $str);
?>

Recommended tutorial: PHP video tutorial

The above is the detailed content of What function does php use to remove leading and trailing spaces?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:How to debug phpNext article:How to debug php