简体   繁体   English

从字符串中提取php数字

[英]Extract php numbers from strings

I need to extract numbers from strings by ignoring 0 's and separate to three string numbers.我需要通过忽略0并分隔为三个字符串数字来从字符串中提取数字。

Numbers goes from 1 to 26 max数字从 1 到 26 最大

Example:例子:

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

19 0 22 0 16 19 0 22 0 16

19 00 2 00 7 19 00 2 00 7

3 0 15 0 10 3 0 15 0 10

FIXED : Not so obvious, tho.固定:不那么明显,不过。 That's the first solution that came to my mind这是我想到的第一个解决方案

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

The above should result in something about:以上应该导致一些关于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM