note 99868 added to function.parse-ini-file

From: Date: Sat, 11 Sep 2010 16:42:31 +0000
Subject: note 99868 added to function.parse-ini-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-172483@lists.php.net to get a copy of this message
a ini lexer with regexp: <?php @header('Content-Type: text/plain'); $myini = <<<EOT [examples] ; this is a section ; this is a comment line 1 = intkey ; this is a int key nullvalue = null ; this is NULL truebool = true ; this is boolean (TRUE) falsebool = false ; this is boolean (FALSE) intvalue = -1 ; this is a integer (-1) floatvalue = +1.4E-3 ; this is a float (0.0014) stringvalue = Hello World ; this is a unquoted string quoted = "Hello World" ; this is a quoted string apostrophed = 'Hello World' ; this is a apostrophed string quoted escaped = "it work's \"fine\"!" ; this is a quoted string with escaped quotes apostrophed escaped = 'it work\'s "fine"!' ; this is a apostrophed string with escaped apostrophes [[valid special cases]] ; this is a section with square brackets and whitespaces at the beginning quoted multiline = "line1 line2 line3" ; this is a quoted multiline string apostrophed multiline = "line1 line2 line3" ; this is a apostrophed multiline string spaces before key = is ok ; this line has whitespaces at the beginning no val = ; this setting has no key = no key ; this setting has no value = ; this setting has no key and no value [bad cases] ; you should never do that but it works notgood = unquoted"string ; this value has a single quote notgood2 = unapostrophed'string ; this value has a single apostrophe bad = "unclosed quotes ; this value has unclosed quotes bad2 = 'unclosed apostrophes ; this value has unclosed apostrophes [invalid section] invalid setting EOT; function get_tokens_from_ini_lexer($data, $verbose = FALSE) { $regexp = '/ (?<=^|\r\n|\r|\n) (?P<line> (?: (?(?![\t\x20]*;) (?P<left_space>[\t\x20]*) (?: \[(?P<section>[^;\r\n]+)\] | (?P<setting> (?P<key> [^=;\r\n]+? )? (?P<left_equal_space>[\t\x20]*) (?P<equal_sign>=) (?P<right_equal_space>[\t\x20]*) (?P<val> \x22(?P<quoted>.*?)(?<!\x5C)\x22 | \x27(?P<apostrophed>.*?)(?<!\x5C)\x27 | (?P<null>null) | (?P<bool>true|false) | (?P<int>[+-]?(?:[1-9]\d{0,18}|0)) | (?P<float>(?:[+-]?(?:[1-9]\d*|0))\.\d+(?:E[+-]\d+)?) | (?P<string>[^;\r\n]+?) )? ) ) ) (?P<right_space>[\t\x20]*) (?: (?P<comment_seperator>;) (?P<comment_space>[\t\x20]*) (?P<comment>[^\r\n]+?)? )? ) | (?P<error> [^\r\n]+? ) ) (?=\r\n|\r|\n|$)(?P<crlf>\r\n|\r|\n)? | (?<=\r\n|\r|\n)(?P<emptyline>\r\n|\r|\n) /xsi'; if(!@is_int(preg_match_all($regexp, $data, $tokens, PREG_SET_ORDER))) { // parse error } else { foreach($tokens as $i => $token) { if(!$verbose) { unset($tokens[$i]['line']); unset($tokens[$i]['crlf']); unset($tokens[$i]['setting']); unset($tokens[$i]['equal_sign']); unset($tokens[$i]['val']); unset($tokens[$i]['left_space']); unset($tokens[$i]['left_equal_space']); unset($tokens[$i]['right_equal_space']); unset($tokens[$i]['right_space']); unset($tokens[$i]['comment_seperator']); unset($tokens[$i]['comment_space']); }; foreach($token as $key => $val) { if(!@is_string($key) || !@strlen($val)) { unset($tokens[$i][$key]); }; }; }; return($tokens); }; }; $verbose = FALSE; print_r(get_tokens_from_ini_lexer($myini, $verbose)); ?> ---- Server IP: 85.214.154.209 Probable Submitter: 93.211.212.35 ---- Manual Page -- http://www.php.net/manual/en/function.parse-ini-file.php Edit -- https://master.php.net/note/edit/99868 Del: integrated -- https://master.php.net/note/delete/99868/integrated Del: useless -- https://master.php.net/note/delete/99868/useless Del: bad code -- https://master.php.net/note/delete/99868/bad+code Del: spam -- https://master.php.net/note/delete/99868/spam Del: non-english -- https://master.php.net/note/delete/99868/non-english Del: in docs -- https://master.php.net/note/delete/99868/in+docs Del: other reasons-- https://master.php.net/note/delete/99868 Reject -- https://master.php.net/note/reject/99868 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172483) next »