PHP: Cut first and last Character from String
Question by Guest | 2015-06-25 at 20:24
I would like to remove the first and the last character from the beginning and from the end of an arbitrary string.
The characters can be of any kind (characters or numbers) and also the length of the string is arbitrary and can vary.
Is there any general PHP function that can shorten my string in this away?
Related Topics
PHP: Read out n-th Character from behind of String
Question | 1 Answer
PHP: Cut out last Character from String
Question | 1 Answer
PHP: Remove last Character from String
Question | 1 Answer
PHP: Iterate UTF-8 String Character by Character
Question | 3 Answers
JavaScript: Remove last character from string
Tip | 5 Comments
PHP: Count specific character in string
Tip | 0 Comments
PHP: Remove first Character from String
Question | 1 Answer
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
You can use the PHP function substr() for cutting off your string at the front and at the back.
In this example, we are transforming the string "abcde" into "bcd". You can remove the first and the last character by passing 1 as second parameter and -1 as third parameter to substr().
The second parameter is determining at which position the resulting substring should start. Because the first character within a string has the position 0, we are passing 1 so that we can get all characters after the first character.
Because we do not know how long our sub string will become exactly, we are using -1 as third parameter. A negative value is ensuring that the characters are counted from behind and with -1 we are cutting off exactly one character from the end of the string.
2015-06-27 at 22:44
Great! Thanks sir! The answer really simple and based on my problem. And Thanks! This solved my problem!
2018-09-28 at 03:35
Thanks, mate! It works good. This solved my problem! )
2019-03-26 at 12:52
How About This
echo substr($s, 0, 0); // ..??
2019-04-27 at 07:05