note 97886 deleted from function.sprintf by danbrown
| From: | danbrown@php.net | Date: | Fri, 14 May 2010 15:09:16 +0000 |
| Subject: | note 97886 deleted from function.sprintf by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169260@lists.php.net to get a copy of this message | ||
Note Submitter: jpic
----
This function still doesn't support replace with named arguments; ie:
<?php
sprintf( "%(foo)s %(bar)s %(foo)s", array(
'foo' => 'FOO',
'bar' => 'BAR',
));
?>
This little php function emulates this feature which is available in other scripting languages:
<?php
function dictionnaryReplace( $string, $dictionnary ) {
$finalDictionnary = array( );
foreach( $dictionnary as $key => $value ) {
$finalDictionnary["%($key)s"] = $value;
}
$string = str_replace(
array_keys( $finalDictionnary ),
array_values( $finalDictionnary ),
$string
);
return $string;
}
?>
Far from perfect.