Explode only by last delimiter

explode php
php split string after character
delimiter in php
php substring from last occurrence
php explode keep delimiter
explode last item
explode in laravel
explode in sql

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:

$expoldeResultArray[0] is "one_two_three ...";

$expoldeResultArray[1] is "four";

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"

Exploding a string, only at the last occurrence of the explode match , The regular expression matches the last delimiter with the last item, but captures only the item so that it's kept in the results thanks to  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()):

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

explode - Manual, If delimiter is an empty string (""), explode() will return FALSE . If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned. Explode does not parse a string by delimiters, in the sense that we expect to find tokens between a starting and ending delimiter, but instead splits a string into parts by using a string as the boundary of each part. Once that boundary is discovered the string is split.

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

split - Manual, Returns an array of strings, each of which is a substring of string formed by splitting it on Remove first and last quotes, then merge pairs of quotes Ups! It seems that neither explode nor split REALY takes a STRING but only a single  Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

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"

Explode only by last delimiter, is there a way to use explode function to explode only by last delimiter occurrence? $string = "one_two_ _three_four"; $explodeResultArray  If your delimiter consists of 2 characters, e.g. a comma and a space, then supply only the comma (",") to the SEARCH function, and use +2 and -2 instead of +1 and -1. How to split string by line break in Excel

<?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);

PHP explode() – split a string by string into array, PHP explode() function is used for split a string by another string into an array. Here delimiter string will declare at which string occurrence the split process will be the LIMIT value and the rest of string will be the last element in array. We want the string between the first and second occurrence of the delimiter, which we can accomplish by wrapping street2 and street3 in annother SUBSTRING_INDEX call. Now we can tell it to retrieve the everything to the right of the last occurrence of the delimiter. Our query now looks like this:

Python, One of the interesting variation of list splitting can be splitting the list on delimiter but this time only on the last occurrence of it. Let's discuss certain ways in which  Text specified in delimiter does not appear in the output C. Specify multiple delimiters in a cell array or a string array. The strsplit function splits str on the elements of delimiter. The order in which delimiters appear in delimiter does not matter unless multiple delimiters begin a match at the same character in str.

Split string or character vector at specified delimiter, In this case, the last character vector in C is empty. This empty character vector follows the last matched delimiter. Split Path on File Separator. This is to avoid having to re-write the filters inside the another measure again, and only define them in ONE measure. I have a DAX measure that returns a string that has concatenated values. like this "1,2,3,4,5,6,7" so far so good. These are the values I want to get access to, but as a list, not as a string.

Split strings at delimiters - MATLAB split, Split names in a string array at whitespace characters. Then reorder the strings and join them so that the last names precede the first names. Create a 3-by-1 string  im sure that is because i am using '' as my delimeter but im trying to explode every character into an array im pretty sure i did this before so im unsure why it wont work now. any thoughts? thanks.

Comments
  • How about explode() and implode() again Excluding the last part!!!
  • I like the String reverse option - very simple to understand and low on CPU.
  • @MichaelCoxon but it can be painful with utf-8 strings.
  • Thank you for answer!
  • Probably the fastest solution