note 84512 modified in function.preg-split by danbrown

From: Date: Wed, 27 May 2009 13:48:55 +0000
Subject: note 84512 modified in function.preg-split by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156064@lists.php.net to get a copy of this message
<?php $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. --was-- <? $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. http://php.net/manual/en/function.preg-split.php

« previous php.notes (#156064) next »