开发者

php regex pattern help -

I'm trying to wrap my head around regex, but would appreciate some help on this.

The given string is "38min *08min 38 *08min 36 *01min *26 50 *15min *41 *11min开发者_如何学JAVA *41"

Trying to parse this string in PHP so that:

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

I need an output something like this.


$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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜