Reputation: 392
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
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;
}
Upvotes: 1
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
)
Upvotes: 3
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
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
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
Reputation: 638
check this :
$tmp[0]=substr($var, 0, -1);
$tmp[1]=substr($var,-1);
print_r($tmp);
Enjoy ;)
Upvotes: 1
Reputation: 1111
$source = "12345E";
print_r(array(substr($source, 0, strlen($source) -1), $source[strlen($source) -1]));
Upvotes: 0