PHP:除了并行结构之外,用逗号分隔一个句子

问题描述:

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?

我想将一个setnence分成逗号中的部分,除非它包含一个paralllel结构。 p>

例如,给出这些句子(http://owl.english.purdue.edu/owl/resource/623/01/):

nn

玛丽喜欢徒步旅行,游泳和骑自行车。 p> blockquote>

我会将它们沿着第一个逗号分开 我会得到: p>

  sentence_array(“玛丽喜欢徒步旅行”,“游泳,骑自行车”)
  code>  pre> 
 
  

也许有一个前瞻性的正则表达式,检查至少2-3个没有用逗号包围的空格? p> div>

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 ] )