note 25099 added to function.preg-split

From: Date: Tue, 10 Sep 2002 19:57:48 +0000
Subject: note 25099 added to function.preg-split
Groups: php.notes 
Request: Send a blank email to php-notes+get-36478@lists.php.net to get a copy of this message
To split a string at the beginning of each line, use a positive lookahead with preg_split. Note, preg_split must have the proper LIMIT parameter for this to work correctly, as follows: <?php function SplitNow($split_pat,$s,$n) { $vals = preg_split($split_pat,$s,$n); $i=0; foreach ($vals as $v) { echo "$i. $v <br>\n"; $i++; } echo '<p><pre>'; print_r($vals); echo '</pre></p>'; } $str = "Are roses red?\nAre violets blue?\nAre flowers green or ever purple?\nAre we having fun yet?\n"; echo "Test string is:<P>\n$str<BR><HR color=navy size=6><P>"; $splitter='/(?=^Are)/m'; // split at pt where line begins with 'Are' $vals = preg_split($splitter,$str); // split into array $num = count($vals); // and get number of elements echo "<BR>LIMIT is $num<P>\n"; // LIMIT should be number of array elements SplitNow($splitter,$str,$num); ?> -- http://www.php.net/manual/en/function.preg-split.php http://master.php.net/manage/user-notes.php?action=edit+25099 http://master.php.net/manage/user-notes.php?action=delete+25099 http://master.php.net/manage/user-notes.php?action=reject+25099

« previous php.notes (#36478) next »