Reputation: 13682
I would like to transform a string into an array of elements delimited by commas or period, but where each element keeps its delimiter. For example, the first sentence to the preamble of the US Declaration of Independence would become:
What would be an efficient way to do that? I know how to save into an array both the pieces and the delimiters, using preg_split
with the PREG_SPLIT_DELIM_CAPTURE
flag, but I'm not sure if there is a way to keep the commas and periods appended to the sentence fragments in one operation.
I guess a more accurate way to describe what I want is a preg_split
where the delimiter would be anything preceded by a comma, a period, etc. I'm not sure whether regex would allow that...
And I need this to be utf8-friendly.
Upvotes: 0
Views: 1981
Reputation: 7525
If I understand you correctly, you are looking for something like the following:
$str = 'asdf, qwer, zxcv, uiop';
$arr = preg_split('/(,)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
$tmp = array();
for ($i = 0; $i < sizeof($arr); $i += 2) {
$delim = isset($arr[$i + 1]) ? $arr[$i + 1] : '';
$tmp[] = $arr[$i] . $delim;
}
print_r($tmp);
Which prints:
Array
(
[0] => asdf,
[1] => qwer,
[2] => zxcv,
[3] => uiop
)
Upvotes: 0
Reputation: 78013
Would this work?
if (preg_match_all("/([^.,!?]*[.,!?])/", $str, $matches)) {
var_dump($matches[1]);
}
Upvotes: 1