Dismantle number with zeroes

Asked

Viewed 76 times

-3

I’d like to undo a number containing zeros and take the integer values.

Example:

0002100042000560000000000000000000000000000000000000000000000000000000000000000000000000000000000000

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

Someone would know me how?

  • 1

    And if the whole number has a zero?

  • 1

    There is no logic to it. Either you use delimited or you will have problems like @Homersimpson said when values are 0.

  • Even if you consider how integers of 1~99 would have problems catching the smaller than 10 with 10.20.30 and so on... to segment them would only be possible if you consider only a range of 10~99

2 answers

5

With regular expression, you achieve 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 string with a minimum size of 1 other than 0.

4

Ta ai a simple solution to your problem:

<?php

$teste = '0002100042000560000000000000000000000000000000000000000000000000000000000000000000000000000000000000';

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

Upshot:

Array ( [3] => 21 [6] => 42 [9] => 56 )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.