Posts tagged string
PHP String Builder
Jan 26th
PHP String Builder using array()
<?php
class TStringBuilder {
/**
* Number of items added to Collection
* @var int
*/
private $count = null;
/**
* String Array
* @var array
*/
private $str = null;
/**
* Initializes a new instance
*/
public function __construct() {
$this->count = 0;
$this->str = array();
}
/**
* Adds new string
* @param string $str
*/
public function add($str) {
++$this->count;
$this->str[] = $str;
return $this;
}
/**
* Clears the string
*/
public function clear() {
$this->count = 0;
$this->str = array();
}
/**
* Gets internal array
*/
public function get() {
return $this->str;
}
/**
* Dumps string
* @param string $seperator
* @return string
*/
public function dump($seperator = "") {
$retVal = "";
foreach ($this->str as $k) {
$retVal .= $k . $seperator;
}
return substr($retVal, 0, strlen($retVal) - strlen($seperator));
}
/**
* Dumps string continiously
*/
public function dumpc() {
foreach ($this->str as $k) {
echo $k;
}
}
/**
* Get number of enteries added
* @return int
*/
public function count() {
return $this->count;
}
/**
* Get string length
* @return int
*/
public function length() {
$retVal = 0;
foreach ($this->str as $k) {
$retVal += strlen($k);
}
return retVal;
}
}


LinkedIn
Twitter
Skype