note 120800 deleted from function.preg-match by cmb
| From: | cmb@php.net | Date: | Sun, 13 Oct 2019 07:56:46 +0000 |
| Subject: | note 120800 deleted from function.preg-match by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212621@lists.php.net to get a copy of this message | ||
Note Submitter: mips at foshin dot com
----
Beware that while a non-matching optional parenthesized subpattern will be stored in $matches as an
empty string ("") if there's at least one match following it, one or more at the end
of the array will be omitted entirely!
Poor example:
<?php
// Slightly modified, this checks for a valid, real number. Here, it stores the dash, whole part,
and period with decimals in $matches.
// Testing ".1":
preg_match("/^(-)?(\\d+)?(\\.\\d+)?\$/", ".1", $matches);
var_dump($matches);
/*
array(4) {
[0]=>
string(2) ".1"
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(2) ".1"
*/
// Testing "-":
preg_match("/^(-)?(\\d+)?(\\.\\d+)?\$/", "-", $matches);
var_dump($matches);
/*
array(2) {
[0]=>
string(1) "-"
[1]=>
string(1) "-"
*/
?>
Notice that $matches[2] here is either "" or not set at all for the exact same result (not
found).