49

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

$string = "one_two_  ... _three_four";

$explodeResultArray = explode("_", $string);

Result Should Be:

echo $explodeResultArray[0]; // "one_two_three ...";
echo $explodeResultArray[1]; // "four";
1

12 Answers 12

107

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"
2
  • 1
    I like the String reverse option - very simple to understand and low on CPU. May 17, 2013 at 14:58
  • 5
    @MichaelCoxon but it can be painful with utf-8 strings.
    – Denis V
    Apr 30, 2017 at 15:03
63

There is no need for a workaround. explode() accepts a negative limit.

$string = "one_two_three_four";
$part   = implode('_', explode('_', $string, -1));
echo $part;

Result is

one_two_three
1
  • 6
    but where is four ?
    – Corentin
    Feb 3, 2022 at 16:11
15

I chose to use substring becasue you want a string up to a particular point:

$string = "one_two_three_four_five_six_seven";
$part1 = substr("$string",0, strrpos($string,'_'));
$part2 = substr("$string", (strrpos($string,'_') + 1));
var_dump($part1,$part2);

RESULTS:

string(27) "one_two_three_four_five_six"
string(5) "seven"
0
10

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

0
6
<?php
$lastPos = strrpos($string, '_');
if ($lastPos !== false) {
    $start = substr($string, 0, $lastPos);
    $end = substr($string, $lastPos+1);
} else {
    // no delimeter found!
}

If you only care about the last part, it's even simpler.

<?php
$end = substr(strrchr($string, '_'), 1);
4

use end + explode

$explodeResultArray = end(explode("_", $string));

$explodeResultArray will = four

8
  • to correct syntax, see stackoverflow.com/a/4636183/2288547 Jan 7, 2020 at 13:15
  • Why is that the correct syntax mine is shorter and does the exact same thing no need to add a variable
    – Avi Teller
    Jan 7, 2020 at 13:49
  • The statement is not valid as is. Have a look at the answer in the link provided. Jan 7, 2020 at 14:51
  • I dunno i just tried it on php 5.3.3 system and it worked no errors $hllo = "whater_sfdd"; $h = end(explode("_", $hllo)); var_dump($h);exit; result is sfdd
    – Avi Teller
    Jan 7, 2020 at 15:48
  • 1
    I'm using xdebug error_reporting = E_ALL which may be why I get the error. Jan 7, 2020 at 16:55
2

Use preg_match()

$string = "one_two_three_four";

$arr = array();
preg_match("/(^.*)_(.*?)$/", $string, $arr);

print_r($arr);

Output: Array ( [0] => one_two_three_four [1] => one_two_three [2] => four )

2
// reverse $string right after definition
$string = "one_two_three_four_five_six";
$string = implode("_",array_reverse(explode("_",$string)));

// chop off the first part
list($result, $string) = explode("_", $string, 2);

echo "$result --- $string";

Output:

six --- five_four_three_two_one 
1
$explodeResultArray = explode("_", $string);
$last_item = end($explodeResultArray);
$key = count($explodeResultArray) - 1;
unset($explodeResultArray[$key]);
$arr[] = (implode($explodeResultArray,'_'));
$arr[] = $last_item;
print_r($arr);

Output

Array
(
    [0] => one_two_  ... _three
    [1] => four
)
1

I had similar needs and inspired by @NLZ's answer I've made a reusable function with the same features as regular explode(), but backwards (although I added an option to reverse the array order contra regular explode()):

function backward_explode($delimiter, $string, $limit = null, $keep_order = true) {
    if ((string)$delimiter === "") {
        return false;
    }

    if ($limit === 0 || $limit === 1) {
        return array($string);
    }

    $explode = explode($delimiter, $string);

    if ($limit === null || $limit === count($explode)) {
        return $keep_order? $explode : array_reverse($explode);
    }

    $parts = array();

    if ($limit > 0) {
        for ($i = 1; $i < $limit; $i++) {
            $parts[] = array_pop($explode);
        }
        $remainder = implode($delimiter, $explode);
        $parts[] = $remainder;
        if ($keep_order) {
            $parts = array_reverse($parts);
        }
    } else {
        if (strpos($string, $delimiter) === false) {
            return array();
        }
        $parts = $explode;
        array_splice($parts, 0, abs($limit));
        if (!$keep_order) {
            $parts = array_reverse($parts);
        }
    }

    return $parts;
}

(Also as a gist.)

So with:

$string = 'one two three four';
var_dump(backward_explode(' ', $string));
var_dump(backward_explode(' ', $string, 2));
var_dump(backward_explode(' ', $string, 3));
var_dump(backward_explode(' ', $string, 2, false));
var_dump(backward_explode(' ', $string, -1));
var_dump(backward_explode(' ', $string, 1)); // same as with $limit = 0
var_dump(backward_explode('#', $string, -2));
var_dump(backward_explode('', $string, 3));

We get:

array (size=4)
  0 => string 'one' (length=3)
  1 => string 'two' (length=3)
  2 => string 'three' (length=5)
  3 => string 'four' (length=4)
array (size=2)
  0 => string 'one two three' (length=13)
  1 => string 'four' (length=4)
array (size=3)
  0 => string 'one two' (length=7)
  1 => string 'three' (length=5)
  2 => string 'four' (length=4)
array (size=2)
  0 => string 'four' (length=4)
  1 => string 'one two three' (length=13)
array (size=3)
  0 => string 'two' (length=3)
  1 => string 'three' (length=5)
  2 => string 'four' (length=4)
array (size=1)
  0 => string 'one two three four' (length=18)
array (size=0)
  empty
boolean false
0

For such a taks, you can just use strstr and strrchr:

$valueBeforeLastUnderscore = rtrim(strrev(strstr(strrev($value), '_')), '_');
$valueAfterLastUnderscore = ltrim(strrchr($value, '_'), '_');

That being said, I like the regular expression answer more.

0

simply use sub string

substr($string ,stripos($string ,'_'))

Not the answer you're looking for? Browse other questions tagged or ask your own question.