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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9677733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:24:33  来源:igfitidea点击:

Extracting last two characters from a numeric string

php

提问by Kinz

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 可以删除这 2 个字符,但是我不确定在删除它们后如何存储它们。

回答by j08691

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

Will store "93" in the array $end

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

回答by capi

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

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

$last2 = $str%100;

回答by kingjeffrey

$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'

回答by Kieran Andrews

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

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