regex preg_match for digit in middle for a string

前端 未结 3 586
走了就别回头了
走了就别回头了 2021-01-29 07:29
ddw_0_op1
ddw_1_op1
ddw_2_op1
ddw_3_op1
ddw_4_op1

in these strings of array how i can extract or preg_match only the number in middle that increase . t

3条回答
  •  醉梦人生
    2021-01-29 08:03

    use the preg_match function:

    $str = "ddw_01_fdfd";
    preg_match('/.+_([0-9]+)_.+/', $str, $matches);
    echo $matches[1];
    

    Output:

    01
    

    Another way to do it is by explode the string by _:

    $xpl = explode('_', $str);
    $middleStr = $xpl[1];
    

    The explode function splits a string by string, in that case create an array made by all substring contained in $str dividing them by _.

    Try it here.

提交回复
热议问题