note 98198 added to function.sprintf

From: Date: Tue, 01 Jun 2010 07:05:30 +0000
Subject: note 98198 added to function.sprintf
Groups: php.notes 
Request: Send a blank email to php-notes+get-169768@lists.php.net to get a copy of this message
function preg_sprintf($format,$params){ $result = ''; $anchor=0; $pattern = '/{([0-9a-zA-Z_]+)}/'; $n = preg_match_all($pattern, $format,$matches, PREG_OFFSET_CAPTURE); for($i=0; $i<$n; $i++){ $result .= substr($format, $anchor, $matches[0][$i][1]-$anchor); if(isset($params[$matches[1][$i][0]])) $result .= $params[$matches[1][$i][0]]; $anchor = $matches[0][$i][1] + strlen($matches[0][$i][0]); } $result .= substr($format, $anchor, strlen($format)-$anchor); return $result; } echo preg_sprintf('f {key} = {value}, {fas}b {key} {efs}',array('fas'=>'FAS', 'key'=>'KEY', 'value'=>'VALUE')); //will echo: f KEY = VALUE, FASb KEY ---- Server IP: 222.73.207.145 Probable Submitter: 127.0.0.1 (proxied: 60.16.233.229) ---- Manual Page -- http://www.php.net/manual/en/function.sprintf.php Edit -- https://master.php.net/note/edit/98198 Del: integrated -- https://master.php.net/note/delete/98198/integrated Del: useless -- https://master.php.net/note/delete/98198/useless Del: bad code -- https://master.php.net/note/delete/98198/bad+code Del: spam -- https://master.php.net/note/delete/98198/spam Del: non-english -- https://master.php.net/note/delete/98198/non-english Del: in docs -- https://master.php.net/note/delete/98198/in+docs Del: other reasons-- https://master.php.net/note/delete/98198 Reject -- https://master.php.net/note/reject/98198 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#169768) next »