从数字字符串中提取最后两个字符

时间:2023-01-04 17:07:02

Okay. Say I have string

好吧。说我有字符串

'193'

And I want to remove the last numbers and store them in an array so I can do operations with them. I know substr can delete the 2 characters, but I'm not sure how to store them after they've been removed..

我想删除最后的数字并将它们存储在一个数组中这样我就可以对它们进行操作。我知道substr可以删除这两个字符,但是我不知道如何在删除后保存它们。

4 个解决方案

#1


32  

$end[] = substr("193", -2);

Will store "93" in the array $end

将“93”存储在数组$end中吗

#2


5  

Why not treat it as a number (your question said it's a numeric string) ?

为什么不把它当作一个数字(你的问题是它是一个数字字符串)?

$last2 = $str%100;

#3


0  

$array = str_split('193'); // $array now has 3 elements: '1', '9', and '3'
array_shift($array); // this removes '1' from $array and leaves '9' and '3'

#4


0  

$str = "193";
$str_array = str_split($str); 

$number_1 = array_pop($str_array); //3
$number_2 = array_pop($str_array); //9

#1


32  

$end[] = substr("193", -2);

Will store "93" in the array $end

将“93”存储在数组$end中吗

#2


5  

Why not treat it as a number (your question said it's a numeric string) ?

为什么不把它当作一个数字(你的问题是它是一个数字字符串)?

$last2 = $str%100;

#3


0  

$array = str_split('193'); // $array now has 3 elements: '1', '9', and '3'
array_shift($array); // this removes '1' from $array and leaves '9' and '3'

#4


0  

$str = "193";
$str_array = str_split($str); 

$number_1 = array_pop($str_array); //3
$number_2 = array_pop($str_array); //9