Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

php

substr

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?

like image 464
Yusuf Avatar asked Jan 30 '26 04:01

Yusuf


2 Answers

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.

like image 146
albertein Avatar answered Jan 31 '26 19:01

albertein


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".

like image 36
lonesomeday Avatar answered Jan 31 '26 19:01

lonesomeday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!