note 81706 added to function.sprintf

From: Date: Mon, 10 Mar 2008 17:13:17 +0000
Subject: note 81706 added to function.sprintf
Groups: php.notes 
Request: Send a blank email to php-notes+get-136906@lists.php.net to get a copy of this message
Was looking for a assoc way of using sprintf but couldnt find one, probably wasnt looking hard enough so came up with this. Very very simple indeed... <?php function sprintf2($str='', $vars=array(), $char='%') { if (!$str) return ''; if (count($vars) > 0) { foreach ($vars as $k => $v) { $str = str_replace($char . $k, $v, $str); } } return $str; } echo sprintf2('Hello %your_name my name is %my_name! I am %my_age, how old are you? I like %object!', array( 'your_name' => 'Ben', 'my_name' => 'Matt', 'my_age' => '21', 'object' => 'food' )); // Hello Ben my name is Matt! I am 21, how old are you? I like food! ?> Looks nice anyway :) ---- Server IP: 87.124.64.96 Probable Submitter: 80.168.248.66 ---- Manual Page -- http://www.php.net/manual/en/function.sprintf.php Edit -- https://master.php.net/note/edit/81706 Del: integrated -- https://master.php.net/note/delete/81706/integrated Del: useless -- https://master.php.net/note/delete/81706/useless Del: bad code -- https://master.php.net/note/delete/81706/bad+code Del: spam -- https://master.php.net/note/delete/81706/spam Del: non-english -- https://master.php.net/note/delete/81706/non-english Del: in docs -- https://master.php.net/note/delete/81706/in+docs Del: other reasons-- https://master.php.net/note/delete/81706 Reject -- https://master.php.net/note/reject/81706 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136906) next »