note 83883 added to function.vsprintf

From: Date: Mon, 16 Jun 2008 20:56:17 +0000
Subject: note 83883 added to function.vsprintf
Groups: php.notes 
Request: Send a blank email to php-notes+get-140514@lists.php.net to get a copy of this message
vnsprintf is equal to vsprintf except for associative, signed or floating keys. vnsprintf supports for example "%assocKey$05d", "%-2$'+10s" and "%3.2$05u", vsprintf doesn't vnsprintf( '%2$d', $array) [2nd value] is equal to vsprintf( '%2$d', $array) [2nd value] vnsprintf( '%+2$d', $array) [key = 2] is equal to vnsprintf( '%2.0$d', $array) [key = 2] vnsprintf( '%+2$d', $array) [key = 2] is different of vsprintf( '%+2$d', $array) [unsupported] When you use signed or floating keys, vnsprintf searchs for the signed truncated key of the original array Note¹: vnsprintf does not support for example "%someKeyf" (floating number, key = someKey) or "%+03d" (signed decimal number, key = 3), you should use "%someKey$f" or "%+03$d" respectively. Note²: "%+03d" (or "%1$+03d") will be interpreted as signed zero-padded decimal number <?php function vnsprintf( $format, array $data) { preg_match_all( '/ (?<!%) % ( (?: [[:alpha:]_-][[:alnum:]_-]* | ([-+])? [0-9]+ (?(2) (?:\.[0-9]+)? | \.[0-9]+ ) ) ) \$ [-+]? \'? .? -? [0-9]* (\.[0-9]+)? \w/x', $format, $match, PREG_SET_ORDER | PREG_OFFSET_CAPTURE); $offset = 0; $keys = array_keys($data); foreach ( $match as &$value ) { if ( ( $key = array_search( $value[1][0], $keys) ) !== FALSE || ( is_numeric( $value[1][0]) && ( $key = array_search( (int)$value[1][0], $keys) ) !== FALSE ) ) { $len = strlen( $value[1][0]); $format = substr_replace( $format, 1 + $key, $offset + $value[1][1], $len); $offset -= $len - strlen( $key); } } return vsprintf( $format, $data); } $examples = array( 2.8=>'positiveFloat', // key = 2 , 1st value -3=>'negativeInteger', // key = -3 , 2nd value 'my_name'=>'someString' // key = my_name , 3rd value ); echo vsprintf( "%%my_name\$s = '%my_name\$s'\n", $examples); // [unsupported] echo vnsprintf( "%%my_name\$s = '%my_name\$s'\n", $examples); // output : "someString" echo vsprintf( "%%2.5\$s = '%2.5\$s'\n", $examples); // [unsupported] echo vnsprintf( "%%2.5\$s = '%2.5\$s'\n", $examples); // output : "positiveFloat" echo vsprintf( "%%+2.5\$s = '%+2.5\$s'\n", $examples); // [unsupported] echo vnsprintf( "%%+2.5\$s = '%+2.5\$s'\n", $examples); // output : "positiveFloat" echo vsprintf( "%%-3.2\$s = '%-3.2\$s'\n", $examples); // [unsupported] echo vnsprintf( "%%-3.2\$s = '%-3.2\$s'\n", $examples); // output : "negativeInteger" echo vsprintf( "%%2\$s = '%2\$s'\n", $examples); // output : "negativeInteger" echo vnsprintf( "%%2\$s = '%2\$s'\n", $examples); // output : [= vsprintf] echo vsprintf( "%%+2\$s = '%+2\$s'\n", $examples); // [unsupported] echo vnsprintf( "%%+2\$s = '%+2\$s'\n", $examples); // output : "positiveFloat" echo vsprintf( "%%-3\$s = '%-3\$s'\n", $examples); // [unsupported] echo vnsprintf( "%%-3\$s = '%-3\$s'\n", $examples); // output : "negativeInteger" ?> ---- Server IP: 64.71.164.2 Probable Submitter: 189.71.57.229 ---- Manual Page -- http://www.php.net/manual/en/function.vsprintf.php Edit -- https://master.php.net/note/edit/83883 Del: integrated -- https://master.php.net/note/delete/83883/integrated Del: useless -- https://master.php.net/note/delete/83883/useless Del: bad code -- https://master.php.net/note/delete/83883/bad+code Del: spam -- https://master.php.net/note/delete/83883/spam Del: non-english -- https://master.php.net/note/delete/83883/non-english Del: in docs -- https://master.php.net/note/delete/83883/in+docs Del: other reasons-- https://master.php.net/note/delete/83883 Reject -- https://master.php.net/note/reject/83883 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#140514) next »