当前位置:网站首页>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!
边栏推荐
猜你喜欢
随机推荐
【HMS core】【FAQ】push kit、分析服务、视频编辑服务典型问题合集3
路遇又一个流量风口,民宿长期向好的逻辑选对了吗
L2-007 家庭房产(vector、set、map的使用)
Classes and Objects (Part 2)
php字符串如何去除第一个字符
Back waves are coming!Ali produced the "second generation" container technical manual and brain map, which is too fragrant
【AGC】Open Test Example
Debug - Notes
tiup help
Troubleshooting TiUP
(Popular Science) What is Fractional NFT (Fractional NFT)
Extremely Knowing v2 Analysis
让人上瘾的新一代开发神器,彻底告别Controller、Service、Dao等方法
70 lines of code, a desktop automatic translation artifact
Delayed message queue
TiUP FAQ
数据库 - 创建数据库、表、函数等
【HMS core】【FAQ】push kit、WisePlay DRM、Location Kit、Health Kit、3D Modeling Kit、SignPal Kit典型问题合集4
When the vite multi-page application refreshes the page, it will not be in the current route and will return to the root route
Promise笔记(一)