note 84512 added to function.preg-split
| From: | anajilly at osu1 dot php dot net | Date: | Thu, 17 Jul 2008 08:17:04 +0000 |
| Subject: | note 84512 added to function.preg-split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-141605@lists.php.net to get a copy of this message | ||
<?
$s = '<p>bleh blah</p><p style="one">one two three</p>';
$htmlbits = preg_split('/(<p( style="[-:a-z0-9 ]+")?>|<\/p>)/i', $s,
-1, PREG_SPLIT_DELIM_CAPTURE);
print_r($htmlbits);
?>
Array
(
[0] =>
[1] => <p>
[2] => bleh blah
[3] => </p>
[4] =>
[5] => <p style="one">
[6] => style="one"
[7] => one two three
[8] => </p>
[9] =>
)
two interesting bits:
1. When using PREG_SPLIT_DELIM_CAPTURE, if you use more than one pair of parentheses, the result
array can have members representing all pairs. See array indexes 5 and 6 to see two adjacent
delimiter results in which the second is a subset match of the first.
2. If a parenthesised sub-expression is made optional by a following question mark (ex: '/abc
(optional subregex)?/') some split delimiters may be captured in the result while others are
not. See array indexes 1 and 2 to see an instance where the overall match succeeded and returned a
delimiter while the optional sub-expression '( style="[-:a-z0-9 ]+")?' did not
match, and did not return a delimiter. This means it's possible to have a result with an
unpredictable number of delimiters in the result array.
This second aspect is true irrespective of the number of pairs of parentheses in the regex. This
means: in a regular expression with a single optional parenthesised sub-expression, the overall
expression can match without generating a corresponding delimiter in the result.
----
Server IP: 209.41.74.194
Probable Submitter: 69.229.223.91
----
Manual Page -- http://www.php.net/manual/en/function.preg-split.php
Edit -- https://master.php.net/note/edit/84512
Del: integrated -- https://master.php.net/note/delete/84512/integrated
Del: useless -- https://master.php.net/note/delete/84512/useless
Del: bad code -- https://master.php.net/note/delete/84512/bad+code
Del: spam -- https://master.php.net/note/delete/84512/spam
Del: non-english -- https://master.php.net/note/delete/84512/non-english
Del: in docs -- https://master.php.net/note/delete/84512/in+docs
Del: other reasons-- https://master.php.net/note/delete/84512
Reject -- https://master.php.net/note/reject/84512
Search -- https://master.php.net/manage/user-notes.php