Explode only by last delimiter

后端 未结 11 1727
隐瞒了意图╮
隐瞒了意图╮ 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:16

    You could do the following:

    $string = "one_two_three_four";
    $explode = explode('_', $string); // split all parts
    
    $end = '';
    $begin = '';
    
    if(count($explode) > 0){
        $end = array_pop($explode); // removes the last element, and returns it
    
        if(count($explode) > 0){
            $begin = implode('_', $explode); // glue the remaining pieces back together
        }
    }
    

    EDIT: array_shift should have been array_pop

提交回复
热议问题