我想将一个setnence分成逗号中的部分,除非它包含一个paralllel结构。
例如,给出这些句子(http://owl.english.purdue.edu/owl/resource/623/01/):
玛丽喜欢徒步旅行,游泳和骑自行车。
玛丽喜欢徒步旅行,游泳,骑自行车。
我会将它们沿着第一个逗号分开,所以我会得到:
sentence_array ( "Mary likes to hike", "swim, and ride a bicycle")
也许有一个前瞻性的正则表达式,检查至少2-3个不用逗号包围的空格?
答案 0 :(得分:2)
也许这样的事情可行:
<?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);
?>
必须使用比仅仅*
答案 1 :(得分:0)
我很难理解你想要的是什么? 爆炸(',','玛丽喜欢徒步旅行,游泳,骑自行车',2)工作?因此2在那里(限制) 或者你想通过','爆炸?
答案 2 :(得分:0)
使用explode(separator,string,limit)
或
split ( string $pattern , string $string [, int $limit = -1 ] )