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

How to remove the right space in php

青灯夜游
Release: 2023-03-11 13:52:02
Original
2151 people have browsed it

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)
Copy after login
##ParameterDescriptionRequired. 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.

<?php
header("Content-type:text/html;charset=utf-8");
$str = " PHP中文网 \n";
var_dump(rtrim($str));
echo &#39;<br>&#39;;
$str = &#39;@_@ https://www.php.cn/@ @_&#39;;
var_dump(rtrim($str,&#39;@ _&#39;));
echo &#39;<br>&#39;;
$str = "123PHP 教程456";
var_dump(rtrim($str,&#39;1..6&#39;));
?>
Copy after login
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!

Related labels:
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