PHP: split a sentence along commas, except for parallel structures
I would like to split a setnence into parts along commas, except if it contains a paralllel structure.
For example, given these sentences (http://owl.english.purdue.edu/owl/resource/623/01/):
Mary likes to hike, to swim, and to ride a bicycle.
Mary likes to hike, swim, and ride a bicycle.
I would split these along the first comma only so I would get:
开发者_运维技巧sentence_array ( "Mary likes to hike", "swim, and ride a bicycle")
Perhaps with a forward looking regex, checking for at least 2-3 white spaces not surrounded by a comma?
Maybe something like this could work:
<?php
$str = "Mary likes to hike, to swim, and to ride a bicycle, also, something more at the end.";
var_dump($str);
$str = preg_replace('/((\s\w*){3,},)/', '\1*', $str);
$str = explode('*', $str);
var_dump($str);
?>
It has to be worked on tho like using something more unique than a mere *
I'm having a hard time understand what is actually that you want? Would explode(',', 'Mary likes to hike, swim, and ride a bicycle', 2) work? hence the 2 in there (limit) Or is it that you want to explode by ', ' ?
use explode(separator,string,limit)
or
split ( string $pattern , string $string [, int $limit = -1 ] )
精彩评论