Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pop last char in a string

Tags:

php

I have this string: 13.23E. What I need is to cut the letter E (or any last letter) to obtain two vars, one with the number, on with the letter.

Example:

$var = "12345E";
print_r(removeLastLetter($var));

// OUTPUT
array(
  [0] => "12345",
  [1] => "E"
)

Any help?
Thanks.

like image 578
Xriuk Avatar asked Dec 31 '25 01:12

Xriuk


1 Answers

function removeLastLetter($string) 
{
    $part1 = substr($string, 0, -1); // get chars upto last
    $part2 = substr($string, -1); // get last char

    return array($part1, $part2);
}

Output:

Array
(
    [0] => 12345
    [1] => E
)

Demo!

like image 61
Amal Murali Avatar answered Jan 01 '26 15:01

Amal Murali



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!