Hi,
I need a regex to split text into an array of sentences. For example, the below is the text:
$text = "aaa bbb (ccc ddd (eee fff) ggg (xxx yyy zzz)) hhh iii jjj (kkk lll mmm) nnn ooo ppp";
I want to split it into an array like this:
[0] => "aaa bbb"
[1] => "(ccc ddd (eee fff) ggg (xxx yyy zzz))"
[2] => "hhh iii jjj"
[3] => "(kkk lll mmm)"
[4] => "nnn ooo ppp"
The basic idea is: a sentence is either inside a pair of brackets, or outside the brackets. A pair of brackets could be nested inside another pair of brackets, but only the most exterior brackets are considered as a sentence.
The following is my code which does not work properly:
<?
$text = "aaa bbb (ccc ddd (eee fff) ggg (xxx yyy zzz)) hhh iii jjj (kkk lll mmm) nnn ooo ppp";
$pattern = "/((.*?))|([()])+/";
preg_match_all($pattern, $text, $matches);
var_dump($matches[0]);
?>
Anyone please help me.
Thanks in advance.