note 120149 added to function.preg-split
| From: | php at haravikk dot me | Date: | Fri, 11 Nov 2016 15:24:19 +0000 |
| Subject: | note 120149 added to function.preg-split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-207073@lists.php.net to get a copy of this message | ||
When using the PREG_SPLIT_OFFSET_CAPTURE option you will end up with all results in a single array,
which is often undesirable as it means you then have to filter out any delimiters you wanted to
check for but not keep.
To get around this you can instead use preg_match_all() to perform the split. For comparison, here
are two examples, both splitting around colon and semi-colon characters:
<?php $pieces_with_delimiters = preg_split('/[;:]/', $input, -1,
PREG_SPLIT_OFFSET_CAPTURE); ?>
<?php preg_match_all('/([^;:]*)([;:]|$)/', $input, $matches);
list(, $pieces, $delimiters) = $matches ?>
The latter requires a more complex pattern, but produces a much more convenient set of results to
work with, depending upon what you want to do with them.
----
Server IP: 72.52.91.14
Probable Submitter: 82.14.234.204
----
Manual Page -- http://php.net/manual/en/function.preg-split.php
Edit -- https://master.php.net/note/edit/120149
Del: integrated -- https://master.php.net/note/delete/120149/integrated
Del: useless -- https://master.php.net/note/delete/120149/useless
Del: bad code -- https://master.php.net/note/delete/120149/bad+code
Del: spam -- https://master.php.net/note/delete/120149/spam
Del: non-english -- https://master.php.net/note/delete/120149/non-english
Del: in docs -- https://master.php.net/note/delete/120149/in+docs
Del: other reasons-- https://master.php.net/note/delete/120149
Reject -- https://master.php.net/note/reject/120149
Search -- https://master.php.net/manage/user-notes.php