Xriuk
Xriuk

Reputation: 392

Pop last char in a string

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.

Upvotes: 3

Views: 6166

Answers (7)

TarranJones
TarranJones

Reputation: 4232

If this were an array rather than a string you would use array_pop, So I would create a function called string_pop() and add it to my list of helpers.

It would mimic array_pop function but for string.

array_pop works like this

function array_pop(array &$array){

    $last_value = end($array); 
    $last_key = key($array);
    unset($array[$last_key]);
    reset($array);
    return $last_value;
}

So string_pop would look like this

function string_pop(&$str){
    $last_char = substr($str, -1);
    $str = substr($str, 0, -1);
    return $last_char;
}

All you have to do now is put the returned value and the original variable in an array or create a function to do this for you string_pop_array().

function string_pop_array($str) {
    $last_char = string_pop($str);
    return array($str, $last_char);
}


$var = '12345E';
print_r(string_pop_array($var);   

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

Using string_pop() will alter the variable passed to it ($str), this is because it is passed by reference , but by calling the string_pop_array() function the original string ($var) wont be effected, only the $str variable within the functions scope will change.

Just to tie up loose ends here's the equivalent function for an array.

function array_pop_array($arr) {
    $last_element = array_pop($arr);
    return array($arr, $last_element);
}


$arr = array(1,2,3,4,5,'E');
print_r(array_pop_array($arr);    

Array (
    [0] => Array ( 
        [0] => 1 
        [1] => 2 
        [2] => 3 
        [3] => 4 
        [4] => 5  
    )
    [1] => E 
)

and the string equivalent to array_shift

function string_shift(&$str){
    $first_char = substr($str, 0, 1);
    $str = substr($str, 1);
    return $first_char;
}

DEMO

Upvotes: 1

Amal Murali
Amal Murali

Reputation: 76666

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!

Upvotes: 3

rpkamp
rpkamp

Reputation: 811

not a serious answer, just a funny -and very nasty, you've been warned!- way of solving this

$var = '10.25E';
$number = (float)$var;
$letter = substr($var, strlen($number));

Upvotes: 0

bvarga
bvarga

Reputation: 723

Your function:

 function removeLastLetter($var) {
       $ret = array();
       // remove the last character
       $ret[] = substr($var, 0, -1);
       // get the last character
       $ret[] = $var[strlen($var)-1]
       return $ret;
    }

Upvotes: 0

Sutandiono
Sutandiono

Reputation: 1750

You can use substr():

$var = "12345E";

$letter = substr ($var, -1);
$number = substr ($var, 0, -1);

print "Letter is {$letter} and number is {$number}\n";

// Output:
// Letter is E and number is 12345

Upvotes: 1

Morteza Soltanabadiyan
Morteza Soltanabadiyan

Reputation: 638

check this :

$tmp[0]=substr($var, 0, -1);
$tmp[1]=substr($var,-1);

print_r($tmp);

Enjoy ;)

Upvotes: 1

vaibhavmande
vaibhavmande

Reputation: 1111

$source = "12345E";

print_r(array(substr($source, 0, strlen($source) -1), $source[strlen($source) -1]));

Upvotes: 0

Related Questions