How to remove the last character in PHP?

Guanhui
Release: 2023-03-01 12:40:01
Original
3593 people have browsed it

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);
?>
Copy after login

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"
Copy after login

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!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!