Given a string, the task is to split the string based on the last occurrence of a delimiter. In this article, we use the delimiter pipe symbol i.e. “|”.
Consider the below example:
Input: Sam|is|working|hard
Output: String 1: Sam|is|working String 2: hard There are four approaches to split the string, which are described below:
Using strrpos() and substr() functionsThe strrpos() function is used to find the position of the last occurrence of the delimiter ‘|’. If the delimiter is found ($lastDelimiterPos is not false), substr() function is then used to split the string into two parts based on the position of the last delimiter.
Example:
PHP
<?php
$string = "Sam/is/working/hard";
// Find the last occurrence of the
// delimiter '|'
$lastDelimiterPos = strrpos($string, '/');
// Check if the delimiter is found
if ($lastDelimiterPos !== false) {
// Split the string based on the last
// occurrence of the delimiter
$part1 = substr($string, 0, $lastDelimiterPos);
$part2 = substr($string, $lastDelimiterPos + 1);
echo "String 1: $part1\n";
echo "String 2: $part2\n";
} else {
// Handle the case where the delimiter is not found
echo "Delimiter not found in the string.\n";
}
?>
OutputString 1: Sam/is/working
String 2: hard
Using explode() and implode() functionsWe can reverse the string and use explode() function to split the string based on the first occurrence of the delimiter, and then reverse the resulting array using implode() function.
Example:
PHP
<?php
$string = " Sam/is/working/hard";
// Reverse the string
$reversedString = strrev($string);
// Split the reversed string based
// on the first occurrence of '|'
$parts = explode('/', $reversedString, 2);
// Reverse each part back to the
// original order
$part1 = strrev($parts[1]);
$part2 = strrev($parts[0]);
echo "String 1: $part1\n";
echo "String 2: $part2\n";
?>
OutputString 1: Sam/is/working
String 2: hard
Using Regular ExpressionsRegular expressions can be used to match the last occurrence of the delimiter and split the string accordingly.
Example:
PHP
<?php
$string = "Sam/is/working/hard";
// Match the last occurrence of '|'
preg_match('/^(.*)\/(.*)$/', $string, $matches);
// Extract the parts
$part1 = $matches[1];
$part2 = $matches[2];
echo "String 1: $part1\n";
echo "String 2: $part2\n";
?>
OutputString 1: Sam/is/working
String 2: hard
Using strrev() and strpos() FunctionsReverse the string with strrev() function and find the position of the first occurrence of the delimiter using strpos() function, and then split the reversed string.
Example:
PHP
<?php
$string = "Sam/is/working/hard";
// Reverse the string
$reversedString = strrev($string);
// Find the position of the first occurrence of '|'
$firstDelimiterPos = strpos($reversedString, "/");
// Split the reversed string based
// on the first occurrence of '|'
$part1 = substr($reversedString, $firstDelimiterPos);
$part2 = substr($reversedString, 0, $firstDelimiterPos);
// Reverse each part back to the original order
$part1 = strrev($part1);
$part2 = strrev($part2);
echo "String 1: $part1\n";
echo "String 2: $part2\n";
?>
OutputString 1: Sam/is/working/
String 2: hard
Using array_pop() and array_slice() FunctionsAnother method to split a string based on the last occurrence of a delimiter is by using the array_pop() and array_slice() functions. This approach involves converting the string to an array, manipulating the array, and then reconstructing the strings.
Example: This method is efficient and easy to understand, making it suitable for splitting strings based on the last occurrence of a delimiter, especially when working with arrays and their manipulation in PHP.
PHP
<?php
function splitStringByLastDelimiter($input, $delimiter) {
// Convert the string to an array using explode
$parts = explode($delimiter, $input);
// Extract the last part
$lastPart = array_pop($parts);
// Join the remaining parts back into a string
$firstPart = implode($delimiter, $parts);
return [$firstPart, $lastPart];
}
// Sample input
$inputString = "Sam|is|working|hard";
$delimiter = "|";
// Split the string
list($string1, $string2) = splitStringByLastDelimiter($inputString, $delimiter);
echo "String 1: " . $string1 . "\n"; // Output: Sam|is|working
echo "String 2: " . $string2 . "\n"; // Output: hard
?>
OutputString 1: Sam|is|working
String 2: hard
Using strrchr() FunctionThe strrchr() function returns the portion of the string starting from the last occurrence of a specified character. By using this function, we can easily split the string into two parts: the part before the last delimiter and the part after the last delimiter.
Example
PHP
<?php
function splitStringByLastDelimiter($str, $delimiter) {
// Find the last occurrence of the delimiter
$lastPart = strrchr($str, $delimiter);
// Check if the delimiter is found
if ($lastPart !== false) {
// Remove the delimiter from the beginning of $lastPart
$lastPart = substr($lastPart, 1);
// Get the first part of the string
$firstPart = substr($str, 0, strrpos($str, $delimiter));
return [$firstPart, $lastPart];
} else {
// If delimiter is not found, return the original string as first part and empty second part
return [$str, ''];
}
}
// Example usage
$str = "Sam|is|working|hard";
$delimiter = '|';
list($firstPart, $lastPart) = splitStringByLastDelimiter($str, $delimiter);
echo "String 1: " . $firstPart . PHP_EOL;
echo "String 2: " . $lastPart . PHP_EOL;
?>
OutputString 1: Sam|is|working
String 2: hard
|