How to delete the last character in the text? [duplicate]

大城市里の小女人 提交于 2019-12-06 08:15:29

问题


Possible Duplicate:
remove last character from string

I want to delete the last character in the text..

For example

My string: example text
Second : example tex

How?


回答1:


Use substr

$text = 'Example Text';
$text = substr($text, 0, -1)

substr takes three arguments, the original text, start and length, it return the substring from the original text starting at start with the given length. If length is negative, that number of characters will be excluded from the string, that's why whe are using the value -1, to exclude the last char from the string.




回答2:


Use substr, which gets a substring of the original string:

$str = 'example text';
$newStr = substr($str, 0, -1); // "example text"

0 means "start at the beginning", -1 means "go until one character before the end".




回答3:


use this

substr($string, 0, -1);



回答4:


Simple and straight to the point

 
$str[strlen($str) - 1] = ''; 

Does your stuff.

Strings are in actuality an array of characters in php. You can therefore set the last index to an empty string. Using unset() would have been nicer but php doesn't allow unsetting string indices.



来源:https://stackoverflow.com/questions/6636491/how-to-delete-the-last-character-in-the-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!