Explode only by last delimiter

后端 未结 11 1674
隐瞒了意图╮
隐瞒了意图╮ 2021-01-03 17:30

is there a way to use explode function to explode only by last delimiter occurrence?

$string = "one_two_  ... _three_four";

$explodeResultArray = e         


        
11条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-03 18:18

    Straightforward:

    $parts = explode('_', $string);
    $last = array_pop($parts);
    $parts = array(implode('_', $parts), $last);
    echo $parts[0]; // outputs "one_two_three"
    

    Regular expressions:

    $parts = preg_split('~_(?=[^_]*$)~', $string);
    echo $parts[0]; // outputs "one_two_three"
    

    String reverse:

    $reversedParts = explode('_', strrev($string), 2);
    echo strrev($reversedParts[0]); // outputs "four"
    

提交回复
热议问题