Location: PHPKode > scripts > TString > tstring/String.class.php
<?php

/**
 * String
 * @desc Fluently designed String Class with common string operations
 * @example
# formatting
$str = new String("This is a test string");
$str->replace(" ", "_")->camelCased();
echo "\nCAMELCASED:      ";
echo $str;
echo "\nUNDERSCORED:     ";
$str->underScored();
echo $str;

# inserting
$str = new String("This is a test string");
$str->indexOf("test")->insert("statement");

echo "\nINSERTING:       ";
echo $str;

# cutting
$str = new String("This is a test string");
$str->cutBetween(4, 10, "...");

echo "\nCUTTING:         ";
echo $str;

# appending
$str = new String("This is a test string");
$str->append("...");

echo "\nAPPENDING:       ";
echo $str;

# prepending
$str = new String("This is a test string");
$str->prepend("-->");

echo "\nPREPENDING:      ";
echo $str;

# padding
$str = new String("This is a test string");
$str->pad(40, "*");

echo "\nPADDING BOTH:    ";
echo $str;

$str = new String("This is a test string");
$str->padLeft(30, "_");

echo "\nPADDING LEFT:    ";
echo $str;

$str = new String("This is a test string");
$str->padRight(30, "_");

echo "\nPADDING RIGHT:    ";
echo $str;

# combining
$str = new String("This is a test string");
$str->cutBetween(4, 10,"...")->padRight(30, "_");

echo "\nCOMBINING:       ";
echo $str;

# combining
$str = new String("This is a test string");
$bStartsWith = $str->startsWith("This");

echo "\nCHECKS:\n";
echo "StartsWith       ";
echo $bStartsWith;

$str = new String("This is a test string");
$bEndsWith = $str->endsWith("string");

echo "\nEndsWith         ";
echo $bEndsWith;

echo "\nOTHERS\n";
$str = new String();
$str->concat(array("This","is","a","test","string"));

echo "\nConcat           ";
echo $str;

$str = new String();
$str->concat(array("This","is","a","test","string"),"_")->camelCased();

echo "\nCombination      ";
echo $str;

 * @author Thomas Schaefer
 */
class String {

    private $error;
    private $length;
    private $string = "";
    private $array = array();
    private $index = 0;
    private $indexOf = false;

    public function __construct($string="") {
        $this->string = $string;
        $this->Length();
    }

    /**
     * concat
     * @desc concatenate array of strings
     * @param array $array
     * @param string $separator
     * @return void
     */
        public function concat($array, $separator=""){
        $this->string = implode($separator, $array);
        return $this;
    }

    /**
     * Length
     * @return int
     */
    private function Length(){
        $this->length = strlen($this->string);
        return $this;
    }

    /**
     * getLength
     * @return int
     */
    public function getLength(){
        return $this->length;
    }

    /**
     * toUpper
     * @return void
     */
    public function toUpper(){
        $this->string = strtoupper($this->string);
        return $this;
    }

    /**
     * toLower
     * @return void
     */
    public function toLower(){
        $this->string = strtolower($this->string);
        return $this;
    }

    /**
     * split
     * @desc split a string into parts
     * @param string $separator
     * @return void
     */
    public function split($separator){
        $this->array = explode($separator, $this->string);
        return $this;
    }

    /**
     * indexOf
     * @find first occurence position of a string part
     * @param string $find
     * @return void
     */
    public function indexOf($find){
        $this->indexOf = true;
        $this->index = strpos($this->string, $find);
        return $this;
    }

    /**
     * lastIndexOf
     * @desc find last occurence position of a string part
     * @param string $find
     * @return void
     */
    public function lastIndexOf($find){
        $this->indexOf = true;
        $this->index = strrpos($this->string, $find);
        return $this;
    }

    /**
     * padLeft
     * @param int $pad_length
     * @param string $pad_string
     * @return void
     */
    public function padLeft($pad_length, $pad_string="*"){
        $this->string = str_pad($this->string, $pad_length, $pad_string, STR_PAD_LEFT);
        return $this;
    }

    /**
     * padRight
     * @param int $pad_length
     * @param string $pad_string
     * @return void
     */
    public function padRight($pad_length, $pad_string="*"){
        $this->string = str_pad($this->string, $pad_length, $pad_string, STR_PAD_RIGHT);
        return $this;
    }

    /**
     * pad
     * @desc pad string left and right
     * @param int $pad_length
     * @param string $pad_string
     * @return void
     */
    public function pad($pad_length, $pad_string="*"){
        $this->string = str_pad($this->string, $pad_length, $pad_string, STR_PAD_BOTH);
        return $this;
    }

    /**
     * startsWith
     * @param string $search
     * @return bool
     */
    public function startsWith($search){
        return (substr($this->string,0, strlen($search))==$search?true:false);
    }

    /**
     * endsWith
     * @param string $search
     * @return bool
     */
    public function endsWith($search){
        return (substr($this->string,-(strlen($search)))==$search?true:false);
    }

    /**
     * cut
     * @param integer $from
     * @param integer $to optional
     */
    public function cut($from = 0, $to = null) {
        $this->string (String::cutting($this->string, $from, $to));
        return $this;
    }

    /**
     * cutBetween
     * @param integer $minLength
     * @param integer $maxStringLength
     * @param string $placeholder
     * @return void;
     */
    public function cutBetween($minLength = 8, $maxStringLength = 20, $placeHolder = "&#8230;") {
        $string = $this->string;
        if (strlen($string) > $maxStringLength and $minLength * 3 < strlen($string)) {
            $string = String::cutting($string, 0, $minLength) . $placeHolder . String::cutting($string, - $minLength);
            $this->string = $string;
        }
        return $this;
    }

    /**
     * cutting
     * @static
     * @param string $string
     * @param int $from
     * @param int $to
     * @return string
     */
	public static function cutting($string, $from, $to = null) {
		if ($to)
			return substr($string, $from, $to);
		return substr($string, $from);
	}

    /**
     * insert
     * @desc prepending indexOf needed
     * @param string $valueToInsert
     * @return void
     */
    public function insert($valueToInsert){
        if($this->indexOf){
            $this->string = substr($this->string, 0, $this->index) . $valueToInsert . substr($this->string, $this->index);
        } else {
            $this->error = new Exception("insert expects an index.");
        }
        return $this;
    }

    /**
     * append
     * @param string $string
     * @return void
     */
    public function append($string){
        $this->string .= $string;
        return $this;
    }

    /**
     * prepend
     * @param string $string
     * @return void
     */
    public function prepend($string){
        $this->string = $string . $this->string;
        return $this;
    }

    /**
     *
     * @param string $look search string
     * @param string $by replace string
     * @return void
     */
    public function replace($look, $by){
        $this->string = str_replace($look,$by, $this->string);
        return $this;
    }

    /**
     * trim
     * @return void
     */
    public function trim() {
        $this->string = trim($this->string);
        return $this;
    }

    /**
     * underScored
     * @param string $word
     * @return string
     */
    public function underScored() {
        $tmp = String::preplace($this->string, array (
        '/([A-Z]+)([A-Z][a-z])/' => '\\1_\\2',
        '/([a-z\d])([A-Z])/' => '\\1_\\2'
        ));
        $this->string = strtolower($tmp);
        return $this;
    }

    /**
     * camelCased
     * @param string $lower_case_and_underscored_word
     * @return string
     */
    public function camelCased() {
        $this->string = str_replace(" ", "", ucwords(str_replace("_", " ", $this->string)));
        return $this;
    }

    /**
     * helper method
     *
     * @param string $search
     * @param string $replacePairs
     * @return string
     */
    protected static function preplace($search, $replacePairs) {
        return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search);
    }

    /**
     *
     * @return string
     */
    public function __toString(){
        if($this->error instanceof Exception){
            return $this->error;
        } elseif(count($this->array)){
            return $this->array;
        }
        return $this->string;
    }
}
Return current item: TString