How to Explode String Right to Left?

前端 未结 12 794
灰色年华
灰色年华 2020-12-31 03:22
$split_point = \' - \';
$string = \'this is my - string - and more\';

How can i make a split using the second instance of $split_point and not the

12条回答
  •  耶瑟儿~
    2020-12-31 03:47

    I liked Moff's answer, but I improved it by limiting the number of elements to 2 and re-reversing the array:

    $split_point = ' - ';
    $string = 'this is my - string - and more';
    
    $result = array_reverse(array_map('strrev', explode($split_point, strrev($string),2)));
    

    Then $result will be :

    Array ( [0] => this is my - string [1] => and more )
    

提交回复
热议问题