Skip to content

Instantly share code, notes, and snippets.

@onyxraven
Last active November 13, 2019 11:48

Revisions

  1. onyxraven renamed this gist Dec 24, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. onyxraven revised this gist Feb 24, 2012. 1 changed file with 17 additions and 3 deletions.
    20 changes: 17 additions & 3 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,18 @@
    <?php
    function nvsprintf($str, $args) {
    /**
    * Named-Param vsprintf()
    *
    * positional-params based on key name, much the same as positional in sprintf()
    *
    * @link http://php.net/manual/en/function.sprintf.php
    * @link http://www.php.net/manual/en/function.vsprintf.php
    *
    * @param string $str format string - replacements are in %KEY$x format
    * where KEY is the array key from $args and x is the sprintf format
    * @param array $args key-value associative array of replacements
    * @return string
    */
    function nvsprintf($str, array $args) {
    $i = 1;
    foreach ($args as $k => $v) {
    $str = str_replace("%{$k}$", "%{$i}$", $str);
    @@ -9,8 +22,9 @@ function nvsprintf($str, $args) {
    }


    ///example

    ////example
    echo nvsprintf('%name$sFoo bar%val$08dxx', array(
    'name' => 'BARBAR',
    'val' => 33
    ));/// echos BARBARFoo bar00000033xx
    ));//// echos BARBARFoo bar00000033xx
  3. onyxraven revised this gist Feb 24, 2012. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,11 @@ function nvsprintf($str, $args) {
    $i++;
    }
    return vsprintf($str, array_values($args));
    }
    }


    ///example
    echo nvsprintf('%name$sFoo bar%val$08dxx', array(
    'name' => 'BARBAR',
    'val' => 33
    ));/// echos BARBARFoo bar00000033xx
  4. onyxraven revised this gist Feb 24, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    function nvsprintf($str, $args) {
    $i = 1;
    foreach ($args as $k => $v) {
  5. onyxraven created this gist Feb 24, 2012.
    8 changes: 8 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    function nvsprintf($str, $args) {
    $i = 1;
    foreach ($args as $k => $v) {
    $str = str_replace("%{$k}$", "%{$i}$", $str);
    $i++;
    }
    return vsprintf($str, array_values($args));
    }