search
Homephp教程php手册php去掉字符串的最后一个字符substr()的用法

今天项目中用到,去掉字符串中的最后一个字符,所以使用了substr,特整理了下,方便需要的朋友学习下。

今天项目中用到,去掉字符串中的最后一个字符
原字符串1,2,3,4,5,6,
去掉最后一个字符",",最终结果为1,2,3,4,5,6
代码如下:
代码如下:
$str = "1,2,3,4,5,6,";
$newstr = substr($str,0,strlen($str)-1);
echo $newstr;

系统自带的函数也可实现这样的效果,两种方法:
1) substr($str, 0, -1)
2)rtrim($str, ",")

substr
取部份字符串。
语法: string substr(string string, int start, int [length]);
返回值: 字符串
函数种类: 资料处理
内容说明
本函数将字符串 string 的第 start 位起的字符串取出 length 个字符。若 start 为负数,则从字符串尾端算起。若可省略的参数 length 存在,但为负数,则表示取到倒数第 length 个字符。
使用范例
代码如下:

echo substr("abcdef", 1, 3); // 返回 "bcd"
echo substr("abcdef", -2); // 返回 "ef"
echo substr("abcdef", -3, 1); // 返回 "d"
echo substr("abcdef", 1, -1); // 返回 "bcde"
?>

PHP rtrim() 函数
定义和用法
rtrim() 函数从字符串的末端开始删除空白字符或其他预定义字符。同chop()函数。
语法
rtrim(string,charlist)参数 描述
string 必需。规定要转换的字符串。
charlist 可选。规定从字符串中删除哪些字符。
如果未设置该参数,则全部删除以下字符:
"\0" - ASCII 0, NULL
"\t" - ASCII 9, 制表符
"\n" - ASCII 10, 新行
"\x0B" - ASCII 11, 垂直制表符
"\r" - ASCII 13, 回车
" " - ASCII 32, 空格
使用范例
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.