Home  >  Article  >  Backend Development  >  How to remove spaces at the beginning and end of a string in php

How to remove spaces at the beginning and end of a string in php

WBOY
WBOYOriginal
2016-07-25 09:05:271134browse
  1. /*
  2. trim removes spaces from both ends of a string,
  3. rtrim removes spaces from the right of a string,
  4. ltrim removes spaces from the left of a string.
  5. */
  6. echo trim("space")."
    ";
  7. echo rtrim("space")."
    ";
  8. echo ltrim("space")."
    ";
  9. ?>
Copy code

Method 2: Replace with regular expression, more powerful. PHP removes spaces from the beginning and end of a string (including full-width)

  1. $str="   Script Academy bbs.it-home.org   ";
  2. $str = mb_ereg_replace('^( | )+', '', $str);
  3. $str = mb_ereg_replace('(|
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