note 126520 added to function.preg-split
| From: | dewi at dewimorgan dot com | Date: | Wed, 20 Oct 2021 19:35:23 +0000 |
| Subject: | note 126520 added to function.preg-split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217092@lists.php.net to get a copy of this message | ||
Beware that it is not safe to assume there are no empty values returned by PREG_SPLIT_NO_EMPTY, nor
that you will see no delimiters if you use PREG_SPLIT_DELIM_CAPTURE, as there are some edge cases
where these are not true.
<?php
# As expected, splitting a string by itself returns two empty strings:
var_export(preg_split("/x/", "x"));
array (
0 => '',
1 => '',
)
# But if we add PREG_SPLIT_NO_EMPTY, then instead of an empty array, we get the delimiter.
var_export(preg_split("/x/", "x", PREG_SPLIT_NO_EMPTY));
array (
0 => 'x',
)
And if we try to split an empty string, then instead of an empty array, we get an empty string even
with PREG_SPLIT_NO_EMPTY.
var_export(preg_split("/x/", "", PREG_SPLIT_NO_EMPTY));
array (
0 => '',
)
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 66.90.161.34)
----
Manual Page -- https://php.net/manual/en/function.preg-split.php
Edit -- https://main.php.net/note/edit/126520
Del: integrated -- https://main.php.net/note/delete/126520/integrated
Del: useless -- https://main.php.net/note/delete/126520/useless
Del: bad code -- https://main.php.net/note/delete/126520/bad+code
Del: spam -- https://main.php.net/note/delete/126520/spam
Del: non-english -- https://main.php.net/note/delete/126520/non-english
Del: in docs -- https://main.php.net/note/delete/126520/in+docs
Del: other reasons-- https://main.php.net/note/delete/126520
Reject -- https://main.php.net/note/reject/126520
Search -- https://main.php.net/manage/user-notes.php