<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" ⁄>

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

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

corner
 
corner
<myVisitorsMap ⁄> <myVisitorsMap ⁄>

corner
 
 

<A Basic PHP StringBuilder ⁄ >




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



StringBuilders are commonly used in .NET or Java (or at least I use it :)), they're more efficient than concatenating Strings. The StringBuilder initially pre-allocates memory space and if necessary it will add more (if exists available memory), when you have all your elements added, you can instruct StringBuilder to join/ concatenate all those elements and give you a final String.


I'll try to simulate a StringBuilder in PHP. I only implemented a few methods; neither did tweak it for low memory consumption (maybe in a near time future:)), this is just a fairly simple example on how to create a StringBuilder.



With this StringBuilder you can make a few basic operations:

- Append; Insert; Remove; Exchange (switch); Replace; Clear; and a few more;




class.StringBuilder.php
  1. <?php
  2.  
  3. /**
  4. * Class StringBuilder
  5. *
  6. * This class tries to emulate a simple StringBuilder
  7. *
  8. * @author pedrocorreia.net
  9. */
  10. class StringBuilder{
  11. private $_result;
  12.  
  13. /**
  14. * Constructor
  15. *
  16. */
  17. public function __construct(){$this->_result=array();}
  18.  
  19. /**
  20. * Destructor
  21. *
  22. */
  23. public function __destruct(){$this->_ClearElements();}
  24.  
  25. /**
  26. * Append Line
  27. *
  28. * @param String
  29. */
  30. public function Append($value){$this->_AddElement($value);}
  31.  
  32. /**
  33. * Append Line including "\n" at the beginning
  34. *
  35. * @param String
  36. */
  37. public function AppendLine($value){$this->_AddElement("\r\n$value");}
  38.  
  39. /**
  40. * Insert element at a specific position
  41. *
  42. * @param Int Position
  43. * @param String value
  44. */
  45. public function Insert($pos, $value){$this->_AddElement($value,$pos-1);}
  46.  
  47. /**
  48. * Switch Elements
  49. *
  50. * @param Int 1st Position
  51. * @param Int 2nd Position
  52. */
  53. public function Exchange($from, $to){$this->_SwitchElements($from-1,$to-1);}
  54.  
  55. /**
  56. * Update Element
  57. *
  58. * @param String Value
  59. * @param Int Position
  60. */
  61. public function Update($value,$pos){$this->_UpdateElement($pos-1,$value);}
  62.  
  63. /**
  64. * Replace Element (Alias of Update)
  65. *
  66. * @param Int Position
  67. * @param String
  68. */
  69. public function Replace($pos,$value){$this->Update($value,$pos);}
  70.  
  71. /**
  72. * Get number of Elements
  73. *
  74. * @return Int
  75. */
  76. public function Count(){return sizeof($this->_result);}
  77.  
  78. /**
  79. * Get total String Length
  80. *
  81. * @return Int
  82. */
  83. public function Length(){
  84. $count=$this->Count();
  85. $sum=0;
  86. for($i=0;$i<$count;$i++){
  87. $sum+=strlen($this->_GetElement($i));
  88. }
  89.  
  90. return $sum;
  91. }
  92.  
  93. /**
  94. * Get specific Element
  95. *
  96. * @param Int Position
  97. * @return String
  98. */
  99. public function Element($pos){
  100. try{$elem=$this->_GetElement($pos-1);}
  101. catch (Exception $ex){$elem="Exception: $ex";}
  102.  
  103. return $elem;
  104. }
  105.  
  106. /**
  107. * Remove element
  108. *
  109. * @param Int Position
  110. * @return Bool
  111. */
  112. public function Remove($pos){
  113. $res=true;
  114. try{$this->_RemoveElement($pos-1);}
  115. catch (Exception $ex){$res=false;}
  116. return $res;
  117. }
  118.  
  119. /**
  120. * Clear all elements
  121. *
  122. */
  123. public function Clear(){$this->_ClearElements();}
  124.  
  125. /**
  126. * Convert StringBuilder to String
  127. *
  128. * @return String
  129. */
  130. public function ToString(){
  131. $count=$this->Count();
  132. for($i=0;$i<$count;$i++){
  133. $str.=nl2br($this->_GetElement($i));
  134. }
  135.  
  136. return $str;
  137. }
  138.  
  139. /**
  140. * Add/ Insert Element
  141. *
  142. * @param String
  143. * @param Int [Optional] Insert element at specific index
  144. *
  145. * note: when inserting element at specific index, if that index's
  146. * bigger than Elements Count, or invalid, i.e.,
  147. * negative or and inexistent index;
  148. * the value will be appended
  149. */
  150. private function _AddElement($value,$pos=null){
  151. if(!is_numeric($pos)){
  152. $this->_result[]=$value;
  153. }
  154. else{
  155. if(!$this->_IsValidIndex($pos)) $pos=$this->Count();
  156. array_splice($this->_result,$pos,0,$value);
  157. }
  158. }
  159.  
  160. /**
  161. * Update Element
  162. *
  163. * @param Int Index
  164. * @param String Value
  165. */
  166. private function _UpdateElement($pos,$value){
  167. if(!$this->_IsValidIndex($pos)) throw new Exception("OUT_OF_BOUNDS");
  168. $this->_result[$pos]=$value;
  169. }
  170.  
  171. /**
  172. * Get Element
  173. *
  174. * @param Int Index
  175. * @return String
  176. */
  177. private function _GetElement($pos){
  178. if(!$this->_IsValidIndex($pos)) throw new Exception("OUT_OF_BOUNDS");
  179. return $this->_result[$pos];
  180. }
  181.  
  182. /**
  183. * Remove Element
  184. *
  185. * @param Int Index
  186. */
  187. private function _RemoveElement($pos){
  188. if(!$this->_IsValidIndex($pos)) throw new Exception("OUT_OF_BOUNDS");
  189. unset($this->_result[$pos]);
  190. $this->_result=array_values($this->_result);
  191. }
  192.  
  193. /**
  194. * Switch Elements
  195. *
  196. * @param Int 1st Index
  197. * @param Int 2nd Index
  198. */
  199. private function _SwitchElements($pos1,$pos2){
  200. if(!$this->_IsValidIndex($pos1) || !$this->_IsValidIndex($pos2)){
  201. throw new Exception("OUT_OF_BOUNDS");
  202. }
  203. $aux=$this->_GetElement($pos2);
  204. $this->_result[$pos2]=$this->_GetElement($pos1);
  205. $this->_result[$pos1]=$aux;
  206. }
  207.  
  208. /**
  209. * Clear StringBuilder
  210. *
  211. */
  212. private function _ClearElements(){unset($this->_result);}
  213.  
  214. /**
  215. * Check if the index is valid
  216. *
  217. * @param Int Index to examine
  218. * @return Bool
  219. */
  220. private function _IsValidIndex($num){
  221. return ($num<$this->Count() && $num>=0);
  222. }
  223.  
  224. }
  225. ?>





An example file
  1. <?php
  2. include_once("class.StringBuilder.php");
  3.  
  4. $str=new StringBuilder();
  5.  
  6. $str->Append(1);
  7. $str->Append(2);
  8. $str->Append(3);
  9. echo $str->ToString();
  10.  
  11. echo "<br/>__________<br/>";
  12. $str->Insert(1,0);
  13. $str->Insert(5,4);
  14. echo $str->ToString();
  15.  
  16. echo "<br/>__________<br/>";
  17. $str->Replace(1,"Hello I replaced the Value '0'. ");
  18. echo $str->ToString();
  19.  
  20. echo "<br/>__________<br/>";
  21. echo "String Length: {$str->Length()} chars";
  22.  
  23. echo "<br/>__________<br/>";
  24. $str->Remove(2);
  25. echo $str->ToString();
  26.  
  27. echo "<br/>__________<br/>";
  28. $str->Exchange(2,3);
  29. echo $str->ToString();
  30.  
  31. echo "<br/>__________<br/>";
  32. $str->Update(". Change the last element to this new text",$str->Count());
  33. echo $str->ToString();
  34.  
  35. echo "<br/>__________<br/>";
  36. $str->AppendLine("... And I add a new line...");
  37. echo $str->ToString();
  38.  
  39. echo "<br/>__________<br/>";
  40. $i=4;
  41. echo "{$i}th Element={$str->Element($i)}";
  42.  
  43. echo "<br/>__________<br/>";
  44. $str->Clear();
  45. //won't output anything, because we didn't
  46. //add elements since last Clear()
  47. echo $str->ToString();
  48. ?>




The examples above will output the following contents:


123
__________
01234
__________
Hello I replaced the Value '0'. 1234
__________
String Length: 36 chars
__________
Hello I replaced the Value '0'. 234
__________
Hello I replaced the Value '0'. 324
__________
Hello I replaced the Value '0'. 32. Change the last element to this new text
__________
Hello I replaced the Value '0'. 32. Change the last element to this new text
... And I add a new line...
__________
4th Element=. Change the last element to this new text
__________





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









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