当前位置:网站首页>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!
边栏推荐
- Google engineer "code completion" tool; "Transformers NLP" accompanying book code; FastAPI development template; PyTorch model acceleration tool; cutting-edge papers | ShowMeAI News Daily
- yarn的安装及使用教程
- 路遇又一个流量风口,民宿长期向好的逻辑选对了吗
- nodejs环境变量设置
- Data Analysis Tools - DDL operations & DML operations in HQL
- R中按照数字大小进行排序
- When the vite multi-page application refreshes the page, it will not be in the current route and will return to the root route
- Core Topics under Microservice Architecture (2): Design Principles and Core Topics of Microservice Architecture
- Delayed message queue
- Recent learning defragmentation (24)
猜你喜欢

Delayed message queue

【HMS core】【FAQ】push kit、WisePlay DRM、Location Kit、Health Kit、3D Modeling Kit、SignPal Kit典型问题合集4

华为ADS获取转化跟踪参数报错:getInstallReferrer IOException: getInstallReferrer not found installreferrer

70行代码撸一个桌面自动翻译神器

nodejs环境变量设置

FME realizes the method of converting CAD with attribute to SHP data

Excel uses Visual Basic Editor to modify macros

yarn安装详细教程说明、升级教程、修改yarn的全局和缓存目录、yarn基本命令

经典实例分割模型Mask RCNN原理与测试

【HMS core】【FAQ】push kit、分析服务、视频编辑服务典型问题合集3
随机推荐
481-82 (105, 24, 82, 34, 153),
数据库日期类型字段设计,应该如何选择?
让人上瘾的新一代开发神器,彻底告别Controller、Service、Dao等方法
哨兵
yarn的安装及使用教程
The Prospects of the Metaverse and the Four Tracks
tiup completion
2022最新 | 室外单目深度估计研究综述
类和对象(下篇)
QIIME2得到PICRUSt2结果后如何分析
使用 TiUP 命令管理组件
AI遮天传 DL-CNN
数据库 - 创建数据库、表、函数等
TiDB 工具适用场景
ECCV2022 | FPN错位对齐,实现高效半监督目标检测 (PseCo)
php如何去除字符串最后一位字符
Google engineer "code completion" tool; "Transformers NLP" accompanying book code; FastAPI development template; PyTorch model acceleration tool; cutting-edge papers | ShowMeAI News Daily
CAD几个优化设置
【AGC】Open Test Example
Flask之路由(app.route)详解