Home  >  Article  >  Backend Development  >  How to remove the last character in PHP?

How to remove the last character in PHP?

Guanhui
GuanhuiOriginal
2020-06-05 17:17:363628browse

How to remove the last character in PHP?

#How to remove the last character in PHP?

In PHP, you can use the "rtrim()" function to remove the last character. This function is used to delete blank characters or other characters at the end of a string. Its syntax is "rtrim(str,char) ", its parameter str represents the string to be operated on, and the parameter char represents the character to be removed.

Usage example

<?php
$text = "\t\tThese are a few words :) ...  ";
$binary = "\x09Example string\x0A";
$hello  = "Hello World";
var_dump($text, $binary, $hello);
print "\n";
$trimmed = rtrim($text);
var_dump($trimmed);
$trimmed = rtrim($text, " \t.");
var_dump($trimmed);
$trimmed = rtrim($hello, "Hdle");
var_dump($trimmed);
// 删除 $binary 末端的 ASCII 码控制字符
// (包括 0 - 31)
$clean = rtrim($binary, "\x00..\x1F");
var_dump($clean);
?>

The above routine will output:

string(32) "        These are a few words :) ...  "
string(16) "    Example string
"
string(11) "Hello World"
string(30) "        These are a few words :) ..."
string(26) "        These are a few words :)"
string(9) "Hello Wor"
string(15) "    Example string"

Recommended tutorial: " PHP tutorial

The above is the detailed content of How to remove the last character 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