How to Explode String Right to Left?

前端 未结 12 832
灰色年华
灰色年华 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:58

    Assuming you only want the first occurrence of $split_point to be ignored, this should work for you:

    # retrieve first $split_point position
    $first = strpos($string, $split_point);
    # retrieve second $split_point positon
    $second = strpos($string, $split_point, $first+strlen($split_point));
    # extract from the second $split_point onwards (with $split_point)
    $substr = substr($string, $second);
    
    # explode $substr, first element should be empty
    $array = explode($split_point, $substr);
    
    # set first element as beginning of string to the second $split_point
    $array[0] = substr_replace($string, '', strpos($string, $substr));
    

    This will allow you to split on every occurrence of $split_point after (and including) the second occurrence of $split_point.

提交回复
热议问题