Home > Backend Development > PHP Tutorial > 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
Release: 2016-07-25 09:05:27
Original
1209 people have browsed it
  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('(|
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