note 112446 added to function.preg-match

From: Date: Mon, 17 Jun 2013 20:49:32 +0000
Subject: note 112446 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-195257@lists.php.net to get a copy of this message
Be aware of bug https://bugs.php.net/bug.php?id=50887 when using sub patterns: Un-matched optional sub patterns at the end won't show up in $matches. Here is a workaround: Assign a name to all subpatterns you are interested in, and merge $match afterwards with an constant array containing some reasonable default values: <?php if (preg_match('/^(?P<lang>[^;*][^;]*){1}(?:;q=(?P<qval>[0-9.]+))?$/u', 'de', $match)) { $match = array_merge(array('lang' => '', 'qval' => ''), $match); print_r($match); } ?> This outputs: Array ( [lang] => de [qval] => [0] => de [1] => de ) Instead of: Array ( [0] => de [lang] => de [1] => de ) ---- Server IP: 213.172.110.14 Probable Submitter: 88.77.66.173 ---- Manual Page -- http://php.net/manual/en/function.preg-match.php Edit -- https://master.php.net/note/edit/112446 Del: integrated -- https://master.php.net/note/delete/112446/integrated Del: useless -- https://master.php.net/note/delete/112446/useless Del: bad code -- https://master.php.net/note/delete/112446/bad+code Del: spam -- https://master.php.net/note/delete/112446/spam Del: non-english -- https://master.php.net/note/delete/112446/non-english Del: in docs -- https://master.php.net/note/delete/112446/in+docs Del: other reasons-- https://master.php.net/note/delete/112446 Reject -- https://master.php.net/note/reject/112446 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195257) next »