note 81676 added to features.http-auth

From: Date: Sun, 09 Mar 2008 16:22:40 +0000
Subject: note 81676 added to features.http-auth
Groups: php.notes 
Request: Send a blank email to php-notes+get-136870@lists.php.net to get a copy of this message
This example did not work for me too. The problem is only the second branch matches. This is why the trimming was required in the previous post. In addition, if there are spaces in the realm name, the second branch truncates the name. I started from a simple regular expression to at least get the right number of matches: preg_match_all('@(\w+)=[^,]+,?@', $txt, $matches, PREG_SET_ORDER); The next step was to replace [^,]+ with the right sub-patterns to distinguish between quoted and non-quoted values: preg_match_all('@(\w+)=(?:([\'"])([^\'"]+)(?:\2)|(\w+)),?@', $txt, $matches, PREG_SET_ORDER); Branch ([\'"])([^\'"]+)(?:\2) matches quoted values and branch (\w+) matches non-quoted values. The problem with the first branch is that the middle sub-pattern ([^\'"]+) matches both single and double quotes, and the author definitely intended to use back-references to solve it. Unfortunately, I could not figure out how to use back-references inside a character class. From the documentation it does not seem possible and I ended up duplicating ([\'"])([^\'"]+)(?:\2) branch to deal with single and double quotes separately: preg_match_all( '@(\w+)=(?:([\'])([^\']+)(?:\2)|(["])([^"]+)(?:\4)|(\w+)),?@', $txt, $matches, PREG_SET_ORDER); The assignment to the $data array in the foreach loop needs to be changed to reflect different number of sub-patterns: $data[$m[1]] = $m[6] ? $m[6] : ($m[5] ? $m[5] : $m[3]); Note that no trimming is required and the expression handles spaces in quoted values. It would also be interesting to know if it is possible to use back-references inside a character class. ---- Server IP: 208.69.120.35 Probable Submitter: 66.235.3.64 ---- Manual Page -- http://www.php.net/manual/en/features.http-auth.php Edit -- https://master.php.net/note/edit/81676 Del: integrated -- https://master.php.net/note/delete/81676/integrated Del: useless -- https://master.php.net/note/delete/81676/useless Del: bad code -- https://master.php.net/note/delete/81676/bad+code Del: spam -- https://master.php.net/note/delete/81676/spam Del: non-english -- https://master.php.net/note/delete/81676/non-english Del: in docs -- https://master.php.net/note/delete/81676/in+docs Del: other reasons-- https://master.php.net/note/delete/81676 Reject -- https://master.php.net/note/reject/81676 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136870) next »