Click here to Skip to main content
13,145,394 members (77,989 online)
Click here to Skip to main content
Add your own
alternative version

Stats

2.9K views
1 bookmarked
Posted 7 Feb 2011

How to use PHP Magic to create a string builder with append format in PHP.

, 7 Feb 2011
Rate this:
Please Sign up or sign in to vote.
Here is the one i wrote sometime ago/* ---------------------------------------------------------------------------- * Teknober.com - All rights reserved * ---------------------------------------------------------------------------- * File Name : TStringBuilder.php * Section ...
Here is the one i wrote sometime ago


/* ----------------------------------------------------------------------------
 * Teknober.com - All rights reserved
 * ----------------------------------------------------------------------------
 * File Name    : TStringBuilder.php
 * Section      : TStringBuilder
 * Dependicies  : none
 * Description  : Implements TStringBuilder class
 * ----------------------------------------------------------------------------
 * Author       : Fatih P
 * E-mail       : v-fpiris@     .com [removed to avoid spam]
 * ----------------------------------------------------------------------------
 * $LastChangedRevision: 38 $
 * --------------------------------------------------------------------------*/
class TStringBuilder {
    private $str = null;
 
    /**
     * Initializes a new instance
     */
    public function __construct()
    {
        $this->str = array();
    }
 
    /**
     * Adds new string
     * @param string $str
     */
    public function add($str)
    {
        $this->str[] = $str;
    }
 
    /**
     * Clears the string
     */
    public function clear() {
        $this->str = array();
    }
 
    /**
     * Gets internal array
     */
    public function get() {
        $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));
    }
 
    /**
     * Get number of enteries added
     * @return int
     */
    public function count() {
        return count($this->str);
    }
 
    /**
     * Get string length
     * @return int
     */
    public function length() {
        $retVal = 0;
        foreach($this->str as $k) {
            $retVal += strlen($k);
        }
 
        return retVal;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Fatih P.
Software Developer (Senior) Solution Developer
Hungary Hungary
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 7 Feb 2011
Article Copyright 2011 by Fatih P.
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid