Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   PHP string Class (http://www.go4expert.com/articles/php-string-class-t29449/)

pein87 27Jan2013 08:47

PHP string Class
 
Creates a class that acts as a string wrapper similar to Java and C#.

Background



Php doesn't do types so I created a class to act as a wrapper to provide a faux type for strings

The Code



The code is designed so that you can create text variables which have there own methods that you can apply on them in a fashion similar to Java or C#.

PHP Code:

<?php
    
class Text
{
    private 
$text;

    public function 
__construct($val null)
    {
        if(
$val != "" || $val != null)
        {
            
$this->text $val;
        }
        else
        {
            
$this->text "";
        }
    }

    public function 
equals($val)
    {
       return ((
$this->text == $val) ? true false);
    }

    public function 
value()
    {
        return 
$this->text;
    }

    public function 
clear()
    {
        
$this->text "";
    }

    public function 
assign($val)
    {
        
$this->text $val;
    }

    public function 
length()
    {
        
$rtn strlen($this->text);
        return 
$rtn;
    }

    public function 
upperCase($type "all")
    {
        
$type strtolower($type);

        switch(
$type)
        {
            case 
"first":
                
$this->text ucfirst($this->text);
            break;

            case 
"words":
                
$this->text ucwords($this->text);
            break;

            case 
"all":
                
$this->text strtoupper($this->text);
            break;

            default:
                
$this->text strtoupper($this->text);
            break;
        } 
    }

    public function 
lowerCase($type "all")
    {
        
$type strtolower($type);

        switch(
$type)
        {
            case 
"first":
                
$this->text lcfirst($this->text);
            break;

            case 
"all":
                
$this->text strtolower($this->text);
            break;

            default:
                
$this->text strtolower($this->text);
            break;
        }
    }

    public function 
reverse($words TRUE$sep ' ')
    {
        
$tmp;

        if(
$words == TRUE)
        {
            
$tmp explode($sep$this->text);
            
$tmp array_reverse($tmp);
            
$this->text implode($sep$tmp);
        }
        else if(
$words == FALSE)
        {
            
$this->text strrev($this->text);
        }
        else
        {
            
$this->text strrev($this->text);
        }
    }

    public function 
combine($txt)
    {
        
$this->text $this->text $txt;
    }

    public function 
contains($txt)
    {
        
$p strpos($this->text,$txt);
        
$rtn FALSE;

        if(
$p === FALSE)
        {
            
$rtn FALSE;
        }
        else
        {
            
$rtn TRUE;
        }

        return 
$rtn;
    }

    public function 
chunk($start,$end)
    {
        
$rtn "";

        if(!
is_numeric($start) && !is_numeric($end))
        {
            
$rtn "";
        }
        else
        {
            
$rtn substr($this->text,$start,$end);
        }

        return 
$rtn;
    }

    public function 
replace($searchTxt$replacement)
    {
        
$this->text str_replace($searchTxt$replacement$this->text);
    }

    public function 
characters($len 0)
    {
        
$rtn;

        if(
$len 0)
        {
            
$rtn str_split($this->text,$len);
        }
        else
        {
            
$rtn str_split($this->text);
        }

        return 
$rtn;
    }
    public function 
slim($chars null)
    {
        if(
is_null($chars) || $chars != "")
        {
           
$this->text trim($this->text);
        }
        else
        {
            
$this->text trim($this->text,$chars);
        }
    }
}

?>

References



Java and C#


All times are GMT +5.5. The time now is 13:16.