3

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.

7 Answers 7

3
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!

Sign up to request clarification or add additional context in comments.

Comments

1

check this :

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

Enjoy ;)

Comments

1

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 

Comments

1

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

Comments

0
$source = "12345E"; print_r(array(substr($source, 0, strlen($source) -1), $source[strlen($source) -1])); 

Comments

0

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; } 

Comments

0

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)); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.