从字符串中提取php数字

时间:2017-02-02 10:44:13

标签: php string

我需要通过忽略0并从三个字符串数字中分离来提取字符串中的数字。

数字从1到26最多

实施例

12 0 25 00 1 :String 1 = 12 - String 2 = 25 - String 3 = 1 ...

19 0的 22 0的 16

19 00 2 00的 7

第3 0的 15 0的 10

1 个答案:

答案 0 :(得分:-1)

已修复:不太明显,等等。这是我想到的第一个解决方案

$string = '02300103024000100';
$output = array();
// First we replace any '00...' string with just '0'
for($a=strlen($string); $a>0; $a--){
   $zero_replace = '';
   while(strlen($zero_replace) < $a) $zero_replace .= '0';
   $string = str_replace($zero_replace,'0',$string);
}
// Now we remove '0' from beginning and end of string (if they exist)
if(substr($string,0,1) == '0') $string = substr($string,1);
if(substr($string,-1,1) == '0') $string = substr($string,0,strlen($string)-1);
// Finally, explode by '0'
$output = explode('0',$string);
print_r($output);

上述内容应该会产生以下内容:

Array ( [0] => 23 [1] => 1 [2] => 3 [3] => 24 [4] => 1 )