airnet
airnet

Reputation: 2673

Get sequences of numeric values which have a distinctive starting format as a sanitized array of delimited strings

The given string is "38min *08min 38 *08min 36 *01min *26 50 *15min *41 *11min *41"

I'm trying to parse this string in PHP so that I get something like:

arr[0] = "38"
arr[1] = "08,38"
arr[2] = "08, 36"
arr[3] = "01, 26, 50"
arr[4] = "15, 41"
arr[5] = "11, 41"

Upvotes: -1

Views: 64

Answers (4)

mickmackusa
mickmackusa

Reputation: 47883

  1. Remove all asterisks -- they are neither helpful, not needed.
  2. Split the string on each space which is followed by 2 digits then the word min.
  3. Remove all min words from the string.

Code: (Demo)

var_export(
    str_replace(
        'min',
        '',
        preg_split(
            '/ (?=\d{2}min)/',
            str_replace('*', '', $str)
        )
    )
);

Output:

array (
  0 => '38',
  1 => '08 38',
  2 => '08 36',
  3 => '01 26 50',
  4 => '15 41',
  5 => '11 41',
)

Optional: if you actually need commas between numbers in the output, then add them in the last step. Demo

var_export(
    str_replace(
        ['min', ' '],
        ['', ', '],
        preg_split(
            '/ (?=\d{2}min)/',
            str_replace('*', '', $str)
        )
    )
);

Upvotes: 0

mario
mario

Reputation: 145482

Since the * delimiters seemingly are incoherent, I would use an awfully complex regex for that:

preg_match_all('#
        (\d+)min[\s*]+
        (?:
            (\d+)(?!min|\d)
                (?:
                   [\s*]+(\d+)(?!min|\d)
                )?
        )?#x',
        $string, $matches, PREG_SET_ORDER);
print_r($matches);

Gives you:

Array
(
    [0] => Array
        (
            [0] => 38min *
            [1] => 38
        )

    [1] => Array
        (
            [0] => 08min 38
            [1] => 08
            [2] => 38
        )

    [2] => Array
        (
            [0] => 08min 36
            [1] => 08
            [2] => 36
        )

    [3] => Array
        (
            [0] => 01min *26 50
            [1] => 01
            [2] => 26
            [3] => 50
        )

    [4] => Array
        (
            [0] => 15min *41
            [1] => 15
            [2] => 41
        )

    [5] => Array
        (
            [0] => 11min *41
            [1] => 11
            [2] => 41
        )

)

You'll have to reassemble the [1] and [2] and [3] entries for your wanted strings.

Upvotes: 0

David Fells
David Fells

Reputation: 6798

$str = "38min *08min 38 *08min 36 *01min *26 50 *15min *41 *11min *41";
$parts = split('*', $str);

$result = array();
foreach ($parts as $part) {
  $result[] = trim(str_replace('min', ', ', $part));
}

This isn't really a regex issue imo but that code should work.

Upvotes: 1

verdesmarald
verdesmarald

Reputation: 11866

How about something like:

$arr = explode($inStr, '*');
for ($i = 0; $i < sizeof($arr); $i += 1) {
    $arr[$i] = str_replace('min', ',', $arr[$i]);
}

Where your data is in $instr.

Upvotes: 0

Related Questions