De-numbering with zeros

-3

I would like to break down a number containing zeros and get the integer values.

Example:

  

0002100042000560000000000000000000000000000000000000000000000000000000000000000000000000000000000000

I have this number and I need to separate 21, 42 and 56 separately.

Would anyone know how to tell me how?

    
asked by anonymous 25.06.2018 / 19:02

2 answers

5

With regular expression, you can do this in a trivial way:

$text = "0002100042000560000000000000000000000000000000000000000000000000000000000000000000000000000000000000";

if (preg_match_all("/[^0]+/", $text, $matches)) {
    print_r($matches);
}

The result would be:

Array
(
    [0] => Array
        (
            [0] => 21
            [1] => 42
            [2] => 56
        )

)

The expression [^0]+ takes any sequence of characters with a minimum length of 1 other than the 0 character.

    
25.06.2018 / 19:12
4

Here's a simple solution to your problem:

<?php

$teste = '0002100042000560000000000000000000000000000000000000000000000000000000000000000000000000000000000000';

$arrayExplode = explode('0',$teste);
$arrayResultado = array_filter($arrayExplode);
print_r($arrayResultado);

Result:

Array ( [3] => 21 [6] => 42 [9] => 56 )
    
25.06.2018 / 19:07