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)".
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)
Description | |
---|---|
string | Required. Specifies the string to check.|
charlist | Optional. Specifies which characters are removed from a string. If this parameter is omitted, all of the following characters are removed:
|
[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 '<br>'; $str = '@_@ https://www.php.cn/@ @_'; var_dump(rtrim($str,'@ _')); echo '<br>'; $str = "123PHP 教程456"; var_dump(rtrim($str,'1..6')); ?>
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!