note 120800 added to function.preg-match

From: Date: Mon, 13 Mar 2017 20:37:00 +0000
Subject: note 120800 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-208906@lists.php.net to get a copy of this message
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). ---- Server IP: 72.52.91.14 Probable Submitter: 83.209.118.11 ---- Manual Page -- http://php.net/manual/en/function.preg-match.php Edit -- https://master.php.net/note/edit/120800 Del: integrated -- https://master.php.net/note/delete/120800/integrated Del: useless -- https://master.php.net/note/delete/120800/useless Del: bad code -- https://master.php.net/note/delete/120800/bad+code Del: spam -- https://master.php.net/note/delete/120800/spam Del: non-english -- https://master.php.net/note/delete/120800/non-english Del: in docs -- https://master.php.net/note/delete/120800/in+docs Del: other reasons-- https://master.php.net/note/delete/120800 Reject -- https://master.php.net/note/reject/120800 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#208906) next »