Is there way to keep delimiter while using php explode or other similar functions?

I feel this is worth adding. You can keep the delimiter in the "after" string by using regex lookahead to split:

$input = "The address is http://stackoverflow.com/";
$parts = preg_split('@(?=http://)@', $input);
// $parts[1] is "http://stackoverflow.com/"

And if the delimiter is of fixed length, you can keep the delimiter in the "before" part by using lookbehind:

$input = "The address is http://stackoverflow.com/";
$parts = preg_split('@(?<=http://)@', $input);
// $parts[0] is "The address is http://"

This solution is simpler and cleaner in most cases.


You can set the flag PREG_SPLIT_DELIM_CAPTURE when using preg_split and capture the delimiters too. Then you can take each pair of 2‍n and 2‍n+1 and put them back together:

$parts = preg_split('/([.?!:])/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
$sentences = array();
for ($i=0, $n=count($parts)-1; $i<$n; $i+=2) {
    $sentences[] = $parts[$i].$parts[$i+1];
}
if ($parts[$n] != '') {
    $sentences[] = $parts[$n];
}

Note to pack the splitting delimiter into a group, otherwise they won’t be captured.


preg_split with PREG_SPLIT_DELIM_CAPTURE flag

For example

$parts = preg_split("/([\.\?\!\:])/", $string, -1, PREG_SPLIT_DELIM_CAPTURE);

Tags:

Php