<pedrocorreia.net ⁄>
corner
<mySearch ⁄> <mySearch ⁄>

corner
 
corner
<mySnippets order="rand" ⁄> <mySnippets order="rand" ⁄>

corner
 
corner
<myContacts ⁄> <myContacts ⁄>

<email ⁄>


pc@pedrocorreia.net

<windows live messenger ⁄>


pedrojacorreia@hotmail.com

<myCurriculum type="pdf" ⁄>


Download
corner
 
corner
<myBlog show="last" ⁄> <myBlog show="last" ⁄>

corner
 
corner
<myNews show="rand" ⁄> <myNews show="rand" ⁄>

corner
 
corner
<myNews type="cat" ⁄> <myNews type="cat" ⁄>

corner
 
corner
<myQuote order="random" ⁄> <myQuote order="random" ⁄>

De nada vale tentar ajudar aqueles que não se ajudam a si mesmos
corner
 
corner
<myPhoto order="random" ⁄> <myPhoto order="random" ⁄>

<pedrocorreia.net ⁄>
corner
 
corner
<myAdSense ⁄> <myAdSense ⁄>

corner
 
corner
<myVisitorsMap ⁄> <myVisitorsMap ⁄>

corner
 
 

<A Basic PHP String.Format ⁄ >




clicks: 17228 17228 2008-06-25 2008-06-25 goto mySnippets mySnippets php  Download  Bookmark This Bookmark This



Here's another one that I use a lot in .NET, String.Format.

Basically String.Format replaces an item with this format {x} (where X's an int) by its correspondent argument, you can do much more, such as format the item, for example format currency, decimal, dates, etc, etc.



Although this is a PHP snippet, you can get much more valid information in this link and take a glimpse at this method.



Well, unfortunately my String::Format won't be that complex, it will just replace items by its correspondent arguments.

I'll use 2 methods to reach this:
- regex patterns with str_replace;
- and other method only using str_replace;


In the method that uses Regex, preg_match_all will be the function used

Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags .
After the first match is found, the subsequent searches are continued on from end of the last match.

in php manual



In this line:
preg_match_all('/\{(\d+)\}/',self::$_str, $indexes,PREG_SET_ORDER);

All the pattern matches will be saved in an array named $indexes, here's a example:



Array(
    [0] => Array(
            [0] => {5}
            [1] => 5
           )

    [1] => Array(
            [0] => {6}
            [1] => 6
           )

    [2] => Array (
            [0] => {0}
            [1] => 0
           )
)


For example let's see this case:

[0] =>
  Array(
    [0] => {5}
    [1] => 5
  )


The first element in the sub-array tells us what item we'll replace and the second element tells us by what specific argument, confuse? Welcome aboard! ;)



class.String.php
  1. <?php
  2. /**
  3. * Class String
  4. *
  5. * This static class tries to implement String.Format
  6. * by using 2 methods. Both returns the same, however
  7. * one was made using Regex Patterns
  8. *
  9. * @author pedrocorreia.net
  10. */
  11. class String{
  12.  
  13. private static $_str;
  14.  
  15. /**
  16. * Format String using Regex Patterns
  17. *
  18. * @param String
  19. * @return String
  20. */
  21. public static function Format($str){
  22.  
  23. self::$_str=$str;
  24.  
  25. //count arguments that our function received
  26. $count_args=func_num_args();
  27. //check if we have sufficient arguments
  28. if($count_args==1){return $str;}
  29.  
  30. //find all ocurrences that matches the pattern {(numbers)}
  31. //and copy them to an auxiliary array named $indexes
  32. //we'll use PREG_SET_ORDER so that we can get a pair of values
  33. //with all the matches found,
  34. //for example: array[y]=array([0]=>"{x}", [1]=>"x");
  35. preg_match_all('/\{(\d+)\}/',self::$_str, $indexes,PREG_SET_ORDER);
  36.  
  37. $count=sizeof($indexes);
  38.  
  39. //looping through our $indexes will give us
  40. //the elements to replace with
  41. for($i=0;$i<$count;$i++){
  42. $arr=$indexes[$i];
  43.  
  44. //what will we replace, for example {x} (on which x=([0-9]+)
  45. $replace=$arr[0];
  46. //get argument value that will replace our {x}
  47. $arg_pos=$arr[1]+1;
  48.  
  49. // check if we have a valid argument
  50. if($arg_pos>-1 && $arg_pos<$count_args){
  51. //get the argument value
  52. $arg_value=func_get_arg($arg_pos);
  53. //replace {x} with the value of specific argument position
  54. self::$_str=str_replace($replace,$arg_value,self::$_str);
  55. }
  56. }
  57.  
  58. return self::$_str;
  59.  
  60. }
  61.  
  62. /**
  63. * Format String using only str_replace
  64. *
  65. * @param String
  66. * @return String
  67. */
  68. public static function FormatSimpler($str){
  69.  
  70. self::$_str=$str;
  71.  
  72. //count arguments that our function received
  73. $count_args=func_num_args();
  74. //check if we have sufficient arguments
  75. if($count_args==1){return $str;}
  76.  
  77. for($i=0;$i<$count_args-1;$i++){
  78. //get the argument value
  79. $arg_value=func_get_arg($i+1);
  80. //replace {$i} with the value of specific argument position
  81. self::$_str=str_replace("{{$i}}",$arg_value,self::$_str);
  82. }
  83.  
  84. return self::$_str;
  85. }
  86.  
  87. }
  88.  
  89. ?>








Example file:
  1. <?php
  2.  
  3. include_once("class.String.php");
  4.  
  5. $str_format="
  6. {5} {6} {0} {1} {4} {2},{3}
  7. {6} {0} {1} {4} {2}.{3}
  8. {5} {6} shadow of {6} day,{3}
  9. {1} embrace {6} world in gray,{3}
  10. {5} {6} {0} {1} {4} {2}.{3}";
  11.  
  12. $res[]=String::Format($str_format,"sun","will","you","<br/>","set for","and","the");
  13.  
  14. $res[]=String::FormatSimpler($str_format,"sun","will","you","<br/>","set for","and","the");
  15.  
  16. echo <<<OUTPUT
  17. {$res[0]}
  18. <br/>
  19. {$res[1]}
  20. OUTPUT;
  21. ?>




The examples above will output the following contents:


and the sun will set for you,
the sun will set for you.
and the shadow of the day,
will embrace the world in gray,
and the sun will set for you.

and the sun will set for you,
the sun will set for you.
and the shadow of the day,
will embrace the world in gray,
and the sun will set for you.



If you have any doubt or found any error, please drop me an email









clicks: 17228 17228 2008-06-25 2008-06-25 goto mySnippets mySnippets php  Download  Bookmark This Bookmark This