Home>Article>Backend Development> How to remove the right space in php

How to remove the right space in php

青灯夜游
青灯夜游 Original
2021-07-13 17:57:18 2044browse

In PHP, you can use the rtrim() function to remove spaces on the right side of a string. This function can remove whitespace characters and special characters on the right side of a string; the syntax format is "rtrim(string)".

How to remove the right space in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, you can use rtrim () function to remove spaces on the right side of a string.

On websites, when users enter data, they often unintentionally enter extra spaces or other special characters. In some cases, spaces and special characters are not allowed in the string. In this case, the spaces and special characters in the string need to be removed.

PHP provides three functions to remove whitespace characters and special characters on the left and right sides of a string, as shown below:

  • trim() function: remove string Blank characters and special characters on the left and right sides;

  • ltrim() function: Remove blank characters and special characters on the left side of the string;

  • rtrim() function: remove whitespace characters and special characters on the right side of the string.

rtrim() function

rtrim() function can remove the characters at the end of the string. Blank characters or other specified characters, the syntax format is as follows:

rtrim(string,charlist)
##Parameter Description Required. Specifies the string to check. Optional. Specifies which characters are removed from a string. If this parameter is omitted, all of the following characters are removed:
string
charlist
    "\0" - NULL
  • "\t" - Tab
  • "\n" - Newline
  • "\x0B" - vertical tab
  • "\r" - carriage return
  • " " - space
rtrim() function will only process the characters at the end of the string.

[Example]Use the rtrim() function to remove blank characters or special characters at the beginning of the string.

'; $str = '@_@ //m.sbmmt.com/@ @_'; var_dump(rtrim($str,'@ _')); echo '
'; $str = "123PHP 教程456"; var_dump(rtrim($str,'1..6')); ?>

The running results are as follows:

How to remove the right space in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to remove the right space in php. 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