×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
2
Language: PHP
Posted by: Massimo Zappino
Added: Jan 12, 2012 7:40 AM
Modified: Jan 13, 2012 10:20 AM
Views: 96
Tags: string format
Example
echo MyString::format("Hello {0}, my name is {1}", "world", "John");
  1. class MyString {
  2.  
  3.     /**
  4.      * Format string using Regex Patterns
  5.      *
  6.      * @param string
  7.      * @return string
  8.      */
  9.     public static function format($str)
  10.     {
  11.         //count arguments that our function received
  12.         $count_args = func_num_args();
  13.  
  14.         //check if we have sufficient arguments
  15.         if ($count_args == 1) {
  16.             return $str;
  17.         }
  18.  
  19.         //find all ocurrences that matches the pattern {(numbers)}
  20.         //and copy them to an auxiliary array named $indexes
  21.         //we'll use PREG_SET_ORDER so that we can get a pair of values
  22.         //with all the matches found,
  23.         //for example: array[y]=array([0]=>"{x}", [1]=>"x");
  24.         preg_match_all('/\{(\d+)\}/', $str, $indexes, PREG_SET_ORDER);
  25.  
  26.         $count = sizeof($indexes);
  27.  
  28.         //looping through our $indexes will give us
  29.         //the elements to replace with
  30.         for ($i = 0; $i < $count; $i++) {
  31.             $arr = $indexes[$i];
  32.  
  33.             //what will we replace, for example {x} (on which x=([0-9]+)
  34.             $replace = $arr[0];
  35.  
  36.             //get argument value that will replace our {x}
  37.             $arg_pos = $arr[1] + 1;
  38.  
  39.             // check if we have a valid argument
  40.             if ($arg_pos > - 1 && $arg_pos < $count_args) {
  41.  
  42.                 //get the argument value
  43.                 $arg_value = func_get_arg($arg_pos);
  44.  
  45.                 //replace {x} with the value of specific argument position
  46.                 $str = str_replace($replace, $arg_value, $str);
  47.             }
  48.         }
  49.  
  50.         return $str;
  51.     }
  52.  
  53.     /**
  54.      * Format string using only str_replace
  55.      *
  56.      * @param string
  57.      * @return string
  58.      */
  59.     public static function formatSimpler($str)
  60.     {
  61.         //count arguments that our function received
  62.         $count_args = func_num_args();
  63.  
  64.         //check if we have sufficient arguments
  65.         if ($count_args == 1) {
  66.             return $str;
  67.         }
  68.  
  69.         for ($i = 0; $i < $count_args - 1; $i++) {
  70.             //get the argument value
  71.             $arg_value = func_get_arg($i + 1);
  72.  
  73.             //replace {$i} with the value of specific argument position
  74.             $str = str_replace("{{$i}}", $arg_value, $str);
  75.         }
  76.  
  77.         return $str;
  78.     }
  79. }
  80. // example
  81.  
  82. echo MyString::format("Hello {0}, my name is {1}", "world", "John");
  83.