Sunday, April 3, 2011

How to Explode String Right to Left ?

$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 first one. Can I specify somehow a right to left search? Best simple approach?

Basically how do I explode from Right to Left. I want to pick up the last instance of " - ".

Result I need:

$item[0]='this is my - string'; $item[1]='and more';

and not:

$item[0]='this is my'; $item[1]='string - and more';
From stackoverflow
  • If I understand correctly, you want the example case to give you ('this is my - string', 'and more')?

    Built-in split/explode seems to be forwards-only - you'll probably have to implement it yourself with strrpos. (right-left search)

    $idx = strrpos($string, $split_point);
    $parts = array(substr($string, 0, $idx), substr($string, $idx+strlen($split_point)))
    
  • You may use strrev to reverse the string, and then reverse the results back:

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

    Not sure if this is the best solution though.

    Evert : Creative! solution!
    Codex73 : Sounds great,., Wow... I'll try now and see.
    Codex73 : Why use split and not explode? Any difference?
    moff : Oops, I thought I was coding in JavaScript there, I've now corrected it.
    Codex73 : How do I reverse it again?
    Codex73 : I mean reverse the array.
    Codex73 : dont quite also get the array_map usage
    moff : array_map runs strrev over every element in the array, and returns the result. You may also try something like this, which may be simpler: $result = array_reverse(explode($split_point, $string));
  • Why not split on ' - ', but then join the first two array entries that you get back together?

    Codex73 : sounds interesting.
    Codex73 : but i need to split on the last instance always of -.
    SilentGhost : +1, the easiest way
    Alister Bulman : if it is always going to be the last instance, you can find the last match with http://php.net/strrpos, then that whichever side you want to/from that position with substr
  • How about this:

    $parts = explode($split_point, $string);
    $last = array_pop($parts);
    $item = array(implode($split_point, $parts), $last);
    

    Not going to win any golf awards, but it shows intent and works well, I think.

  • 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.

0 comments:

Post a Comment