当前位置:网站首页>How to remove last character from string in php
How to remove last character from string in php
2022-07-30 15:54:00 【Yisuyun】
How php removes the last character of a string
This article mainly introduces "how to remove the last character of a string in php". In daily operations, I believe many people have doubts about how to remove the last character of a string in php.After consulting all kinds of materials, I have sorted out a simple and easy-to-use operation method. I hope it will help you to answer your doubts about "how to remove the last character of a string in php"!Next, please follow the editor to learn together!
Two removal methods: 1. Use substr() to remove, just set the second parameter of the function to 0 and the third parameter to (string length-1), the syntax "substr($str,0,strlen($str)-1)".2. Use str_split() to convert the string into a character array, use array_pop() to delete the last element in the character array, and then use implode() to convert the character array back to a string.

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
Two methods for php to remove the last character of a string
Method 1: Use the substr() function to remove
Thesubstr() function can truncate a certain length of characters from a specified position in a string.
substr(string,start,length)
| parameter | Description |
|---|---|
| string | Required.Specifies the string of which part is to be returned. |
| start | Required.Specifies where in the string to start.
|
| length | Optional.Specifies the length of the string to be returned.The default is until the end of the string.
|
Just set the second parameter of the function to 0 and the third parameter to (string length-1).
";echo "Remove the last character: ".substr($str,0,strlen($str)-1)."
";?>

Method 2: Use str_split()+array_pop()+implode() function
Use the str_split() function to split a string into an array, that is, convert a string to an array of characters.
Use the array_pop() function to delete the last character element in a character array
Use the implode() function to convert the character array back to a string
";$arr=str_split($str);array_pop($arr);echo "Remove the last character: ".implode($arr)."
";?>

At this point, the study on "how to remove the last character of a string in php" is over, and I hope to solve your doubts.The combination of theory and practice can better help everyone learn, go try it!If you want to continue to learn more relevant knowledge, please continue to pay attention to the Yisuyun website, and the editor will continue to work hard to bring you more useful articles!
边栏推荐
- FME读写cass数据的方案及操作流程
- CAD几个优化设置
- 70 lines of code, a desktop automatic translation artifact
- 【AGC】质量服务1-崩溃服务示例
- 90后人大硕士为学医竟重新高考,成功被首医大录取
- Delayed message queue
- EST综述:eDNA的多种状态以及在水环境中持久性的认知
- HTTP缓存小结
- [Cloud Native] Service Industry Case - Solutions for Unpredictable Concurrency Scenarios
- MySql 和 PostgreSQL 数据库 根据一张表update另一张表数据
猜你喜欢

arcpy使用教程

影像信息提取DEM

谷歌工程师『代码补全』工具;『Transformers NLP』随书代码;FastAPI开发模板;PyTorch模型加速工具;前沿论文 | ShowMeAI资讯日报

Configuration - Notes

动态规划 --- 状态压缩DP 详细解释

Local Transactions vs Distributed Transactions

(Popular Science) What is Fractional NFT (Fractional NFT)

在树莓派上驱动CSI摄像头

三维重建方法汇总

【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常
随机推荐
近段时间的学习碎片整理(24)
Golang分布式应用之Redis怎么使用
TiDB 工具下载
解析字符串拼接的两种情况
tiup completion
Store Limit 使用文档
涨姿势了!原来这才是多线程正确实现方式
arcpy使用教程
数据库日期类型字段设计,应该如何选择?
Image information extraction DEM
几种常见的存储器
Load Base Split 使用文档
tiup install
tiup env
Shell脚本的概念
应用OPC解决方案实现控制系统数据的安全交换
(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
481-82 (105, 24, 82, 34, 153),
哨兵
Delayed message queue