TeamSpeak 3 PHP Framework  1.1.12
TeamSpeak3_Helper_String Class Reference

Helper class for string handling. More...

List of all members.

Public Member Functions

 __construct ($string)
 The TeamSpeak3_Helper_String constructor.
 replace ($search, $replace, $caseSensitivity=TRUE)
 Replaces every occurrence of the string $search with the string $replace.
 arg (array $args, $char="%")
 This function replaces indexed or associative signs with given values.
 startsWith ($pattern)
 Returns true if the string starts with $pattern.
 endsWith ($pattern)
 Returns true if the string ends with $pattern.
 findFirst ($needle)
 Returns the position of the first occurrence of a char in a string.
 findLast ($needle)
 Returns the position of the last occurrence of a char in a string.
 toLower ()
 Returns the lowercased string.
 toUpper ()
 Returns the uppercased string.
 contains ($pattern, $regexp=FALSE)
 Returns true if the string contains $pattern.
 substr ($start, $length=null)
 Returns part of a string.
 split ($separator, $limit=0)
 Splits the string into substrings wherever $separator occurs.
 append ($part)
 Appends $part to the string.
 prepend ($part)
 Prepends $part to the string.
 section ($separator, $first=0, $last=0)
 Returns a section of the string.
 resize ($size, $char="\0")
 Sets the size of the string to $size characters.
 trim ()
 Strips whitespaces (or other characters) from the beginning and end of the string.
 escape ()
 Escapes a string using the TeamSpeak 3 escape patterns.
 unescape ()
 Unescapes a string using the TeamSpeak 3 escape patterns.
 filterAlnum ()
 Removes any non alphanumeric characters from the string.
 filterAlpha ()
 Removes any non alphabetic characters from the string.
 filterDigits ()
 Removes any non numeric characters from the string.
 isInt ()
 Returns TRUE if the string is a numeric value.
 toInt ()
 Returns the integer value of the string.
 toCrc32 ()
 Calculates and returns the crc32 polynomial of the string.
 toMd5 ()
 Calculates and returns the md5 checksum of the string.
 toSha1 ()
 Calculates and returns the sha1 checksum of the string.
 isUtf8 ()
 Returns TRUE if the string is UTF-8 encoded.
 toUtf8 ()
 Converts the string to UTF-8.
 toBase64 ()
 Encodes the string with MIME base64 and returns the result.
 toHex ()
 Returns the hexadecimal value of the string.
 spaceToPercent ()
 Replaces space characters with percent encoded strings.
 toString ()
 Returns the string as a standard string.
 __call ($function, $args)
 Magical function that allows you to call PHP's built-in string functions on the TeamSpeak3_Helper_String object.
 __toString ()
 Returns the character as a standard string.
 count ()
 
 rewind ()
 
 valid ()
 
 key ()
 
 current ()
 
 next ()
 
 offsetExists ($offset)
 
 offsetGet ($offset)
 
 offsetSet ($offset, $value)
 
 offsetUnset ($offset)
 

Static Public Member Functions

static factory ($string)
 Returns a TeamSpeak3_Helper_String object for thegiven string.
static fromBase64 ($base64)
 Decodes the string with MIME base64 and returns the result as an TeamSpeak3_Helper_String.
static fromHex ($hex)
 Returns the TeamSpeak3_Helper_String based on a given hex value.

Protected Attributes

 $string
 $position = 0
 

Detailed Description

Helper class for string handling.

Definition at line 32 of file String.php.


Constructor & Destructor Documentation

The TeamSpeak3_Helper_String constructor.

Parameters:
string$string
Returns:
TeamSpeak3_Helper_String

Definition at line 52 of file String.php.

  {
    $this->string = strval($string);
  }

Member Function Documentation

TeamSpeak3_Helper_String::replace ( search,
replace,
caseSensitivity = TRUE 
)

Replaces every occurrence of the string $search with the string $replace.

Parameters:
string$search
string$replace
boolean$caseSensitivity
Returns:
TeamSpeak3_Helper_String

Definition at line 76 of file String.php.

  {
    if($caseSensitivity)
    {
      $this->string = str_replace($search, $replace, $this->string);
    }
    else
    {
      $this->string = str_ireplace($search, $replace, $this->string);
    }

    return $this;
  }
TeamSpeak3_Helper_String::arg ( array $  args,
char = "%" 
)

This function replaces indexed or associative signs with given values.

Parameters:
array$args
string$char
Returns:
TeamSpeak3_Helper_String

Definition at line 97 of file String.php.

Referenced by TeamSpeak3_Exception\prepareCustomMessage().

  {
    $args = array_reverse($args, TRUE);

    foreach($args as $key => $val)
    {
      $args[$char . $key] = $val;
      unset($args[$key]);
    }

    $this->string = strtr($this->string, $args);

    return $this;
  }

Returns true if the string starts with $pattern.

Parameters:
string$pattern
Returns:
boolean

Definition at line 118 of file String.php.

References substr().

Referenced by TeamSpeak3_Adapter_ServerQuery_Event\__construct().

  {
    return (substr($this->string, 0, strlen($pattern)) == $pattern) ? TRUE : FALSE;
  }

Returns true if the string ends with $pattern.

Parameters:
string$pattern
Returns:
boolean

Definition at line 129 of file String.php.

References substr().

  {
    return (substr($this->string, strlen($pattern)*-1) == $pattern) ? TRUE : FALSE;
  }

Returns the position of the first occurrence of a char in a string.

Parameters:
string$needle
Returns:
integer

Definition at line 140 of file String.php.

  {
    return strpos($this->string, $needle);
  }

Returns the position of the last occurrence of a char in a string.

Parameters:
string$needle
Returns:
integer

Definition at line 151 of file String.php.

  {
    return strrpos($this->string, $needle);
  }

Returns the lowercased string.

Returns:
TeamSpeak3_Helper_String

Definition at line 161 of file String.php.

  {
    return new self(strtolower($this->string));
  }

Returns the uppercased string.

Returns:
TeamSpeak3_Helper_String

Definition at line 171 of file String.php.

  {
    return new self(strtoupper($this->string));
  }
TeamSpeak3_Helper_String::contains ( pattern,
regexp = FALSE 
)

Returns true if the string contains $pattern.

Parameters:
string$pattern
booean$regexp
Returns:
boolean

Definition at line 183 of file String.php.

Referenced by isInt().

  {
    if(empty($pattern))
    {
      return TRUE;
    }

    if($regexp)
    {
      return (preg_match("/" . $pattern . "/i", $this->string)) ? TRUE : FALSE;
    }
    else
    {
      return (stristr($this->string, $pattern) !== FALSE) ? TRUE : FALSE;
    }
  }
TeamSpeak3_Helper_String::substr ( start,
length = null 
)

Returns part of a string.

Parameters:
integer$start
integer$length
Returns:
TeamSpeak3_Helper_String

Definition at line 207 of file String.php.

Referenced by endsWith(), resize(), and startsWith().

  {
    $string = ($length !== null) ? substr($this->string, $start, $length) : substr($this->string, $start);

    return new self($string);
  }
TeamSpeak3_Helper_String::split ( separator,
limit = 0 
)

Splits the string into substrings wherever $separator occurs.

Parameters:
string$separator
integer$limit
Returns:
array

Definition at line 221 of file String.php.

References count().

Referenced by TeamSpeak3_Adapter_ServerQuery_Event\__construct().

  {
    $parts = explode($separator, $this->string, ($limit) ? intval($limit) : $this->count());

    foreach($parts as $key => $val)
    {
      $parts[$key] = new self($val);
    }

    return $parts;
  }

Appends $part to the string.

Parameters:
string$part
Returns:
TeamSpeak3_Helper_String

Definition at line 239 of file String.php.

  {
    $this->string = $this->string . strval($part);

    return $this;
  }

Prepends $part to the string.

Parameters:
string$part
Returns:
TeamSpeak3_Helper_String

Definition at line 252 of file String.php.

  {
    $this->string = strval($part) . $this->string;

    return $this;
  }
TeamSpeak3_Helper_String::section ( separator,
first = 0,
last = 0 
)

Returns a section of the string.

Parameters:
string$separator
integer$first
integer$last
Returns:
TeamSpeak3_Helper_String

Definition at line 267 of file String.php.

References count().

Referenced by TeamSpeak3_Adapter_ServerQuery\request(), and TeamSpeak3_Adapter_ServerQuery\wait().

  {
    $sections = explode($separator, $this->string);

    $total = count($sections);
    $first = intval($first);
    $last = intval($last);

    if($first > $total) return null;
    if($first > $last) $last = $first;

    for($i = 0; $i < $total; $i++)
    {
      if($i < $first || $i > $last)
      {
        unset($sections[$i]);
      }
    }

    $string = implode($separator, $sections);

    return new self($string);
  }
TeamSpeak3_Helper_String::resize ( size,
char = "\0" 
)

Sets the size of the string to $size characters.

Parameters:
integer$size
string$char
Returns:
TeamSpeak3_Helper_String

Definition at line 298 of file String.php.

References count(), and substr().

  {
    $chars = ($size - $this->count());

    if($chars < 0)
    {
      $this->string = substr($this->string, 0, $chars);
    }
    elseif($chars > 0)
    {
      $this->string = str_pad($this->string, $size, strval($char));
    }

    return $this;
  }

Strips whitespaces (or other characters) from the beginning and end of the string.

Returns:
TeamSpeak3_Helper_String

Definition at line 319 of file String.php.

  {
    $this->string = trim($this->string);

    return $this;
  }

Escapes a string using the TeamSpeak 3 escape patterns.

Returns:
TeamSpeak3_Helper_String

Definition at line 331 of file String.php.

References TeamSpeak3\getEscapePatterns().

  {
    foreach(TeamSpeak3::getEscapePatterns() as $search => $replace)
    {
      $this->string = str_replace($search, $replace, $this->string);
    }

    return $this;
  }

Unescapes a string using the TeamSpeak 3 escape patterns.

Returns:
TeamSpeak3_Helper_String

Definition at line 346 of file String.php.

References TeamSpeak3\getEscapePatterns().

  {
    $this->string = strtr($this->string, array_flip(TeamSpeak3::getEscapePatterns()));

    return $this;
  }

Removes any non alphanumeric characters from the string.

Returns:
TeamSpeak3_Helper_String

Definition at line 358 of file String.php.

  {
    $this->string = preg_replace("/[^[:alnum:]]/", "",  $this->string);

    return $this;
  }

Removes any non alphabetic characters from the string.

Returns:
TeamSpeak3_Helper_String

Definition at line 370 of file String.php.

  {
    $this->string = preg_replace("/[^[:alpha:]]/", "",  $this->string);

    return $this;
  }

Removes any non numeric characters from the string.

Returns:
TeamSpeak3_Helper_String

Definition at line 382 of file String.php.

  {
    $this->string = preg_replace("/[^[:digit:]]/", "",  $this->string);

    return $this;
  }

Returns TRUE if the string is a numeric value.

Returns:
boolean

Definition at line 394 of file String.php.

References contains().

  {
    return (is_numeric($this->string) && !$this->contains(".")) ? TRUE : FALSE;
  }

Returns the integer value of the string.

Returns:
float
integer

Definition at line 405 of file String.php.

  {
    if($this->string == pow(2, 63) || $this->string == pow(2, 64))
    {
      return -1;
    }

    return ($this->string > pow(2, 31)) ? floatval($this->string) : intval($this->string);
  }

Calculates and returns the crc32 polynomial of the string.

Returns:
string

Definition at line 420 of file String.php.

  {
    return crc32($this->string);
  }

Calculates and returns the md5 checksum of the string.

Returns:
string

Definition at line 430 of file String.php.

  {
    return md5($this->string);
  }

Calculates and returns the sha1 checksum of the string.

Returns:
string

Definition at line 440 of file String.php.

  {
    return sha1($this->string);
  }

Returns TRUE if the string is UTF-8 encoded.

This method searches for non-ascii multibyte sequences in the UTF-8 range.

Returns:
boolean

Definition at line 451 of file String.php.

Referenced by toUtf8().

  {
    //return (utf8_encode(utf8_decode($this->string)) == $this->string) ? TRUE : FALSE;

    $pattern = array();

    $pattern[] = "[\xC2-\xDF][\x80-\xBF]";            // non-overlong 2-byte
    $pattern[] = "\xE0[\xA0-\xBF][\x80-\xBF]";        // excluding overlongs
    $pattern[] = "[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}"; // straight 3-byte
    $pattern[] = "\xED[\x80-\x9F][\x80-\xBF]";        // excluding surrogates
    $pattern[] = "\xF0[\x90-\xBF][\x80-\xBF]{2}";     // planes 1-3
    $pattern[] = "[\xF1-\xF3][\x80-\xBF]{3}";         // planes 4-15
    $pattern[] = "\xF4[\x80-\x8F][\x80-\xBF]{2}";     // plane 16

    return preg_match("%(?:" . implode("|", $pattern) . ")+%xs", $this->string);;
  }

Converts the string to UTF-8.

Returns:
TeamSpeak3_Helper_String

Definition at line 473 of file String.php.

References isUtf8().

  {
    if(!$this->isUtf8())
    {
      $this->string = utf8_encode($this->string);
    }

    return $this;
  }

Encodes the string with MIME base64 and returns the result.

Returns:
string

Definition at line 488 of file String.php.

  {
    return base64_encode($this->string);
  }
static TeamSpeak3_Helper_String::fromBase64 ( base64) [static]

Decodes the string with MIME base64 and returns the result as an TeamSpeak3_Helper_String.

Parameters:
string
Returns:
TeamSpeak3_Helper_String

Definition at line 499 of file String.php.

Referenced by TeamSpeak3_Node_Server\snapshotDeploy().

  {
    return new self(base64_decode($base64));
  }

Returns the hexadecimal value of the string.

Returns:
string

Definition at line 509 of file String.php.

  {
    $hex = "";

    foreach($this as $char)
    {
      $hex .= $char->toHex();
    }

    return $hex;
  }
static TeamSpeak3_Helper_String::fromHex ( hex) [static]

Returns the TeamSpeak3_Helper_String based on a given hex value.

Parameters:
string
Exceptions:
TeamSpeak3_Helper_Exception
Returns:
TeamSpeak3_Helper_String

Definition at line 528 of file String.php.

Referenced by TeamSpeak3_Node_Server\snapshotDeploy(), and TeamSpeak3_Adapter_Update\syn().

  {
    $string = "";

    if(strlen($hex)%2 == 1)
    {
      throw new TeamSpeak3_Helper_Exception("given parameter '" . $hex . "' is not a valid hexadecimal number");
    }

    foreach(str_split($hex, 2) as $chunk)
    {
      $string .= chr(hexdec($chunk));
    }

    return new self($string);
  }

Replaces space characters with percent encoded strings.

Returns:
string

Definition at line 550 of file String.php.

  {
    return str_replace(" ", "%20", $this->string);
  }

Returns the string as a standard string.

Returns:
string

Definition at line 560 of file String.php.

  {
    return $this->string;
  }
TeamSpeak3_Helper_String::__call ( function,
args 
)

Magical function that allows you to call PHP's built-in string functions on the TeamSpeak3_Helper_String object.

Parameters:
string$function
array$args
Exceptions:
TeamSpeak3_Helper_Exception
Returns:
TeamSpeak3_Helper_String

Definition at line 573 of file String.php.

References count().

  {
    if(!function_exists($function))
    {
      throw new TeamSpeak3_Helper_Exception("cannot call undefined function '" . $function . "' on this object");
    }

    if(count($args))
    {
      if(($key = array_search($this, $args, TRUE)) !== FALSE)
      {
        $args[$key] = $this->string;
      }
      else
      {
        throw new TeamSpeak3_Helper_Exception("cannot call undefined function '" . $function . "' without the " . __CLASS__ . " object parameter");
      }

      $return = call_user_func_array($function, $args);
    }
    else
    {
      $return = call_user_func($function, $this->string);
    }

    if(is_string($return))
    {
      $this->string = $return;
    }
    else
    {
      return $return;
    }

    return $this;
  }

Returns the character as a standard string.

Returns:
string

Definition at line 615 of file String.php.

  {
    return (string) $this->string;
  }

Definition at line 623 of file String.php.

Referenced by __call(), resize(), section(), split(), and valid().

  {
    return strlen($this->string);
  }

Definition at line 631 of file String.php.

  {
    $this->position = 0;
  }

Definition at line 639 of file String.php.

References count().

  {
    return $this->position < $this->count();
  }

Definition at line 647 of file String.php.

  {
    return $this->position;
  }

Definition at line 655 of file String.php.

  {
    return new TeamSpeak3_Helper_Char($this->string{$this->position});
  }

Definition at line 663 of file String.php.

  {
    $this->position++;
  }

Definition at line 671 of file String.php.

Referenced by offsetGet(), offsetSet(), and offsetUnset().

  {
    return ($offset < strlen($this->string)) ? TRUE : FALSE;
  }

Definition at line 679 of file String.php.

References offsetExists().

  {
    return ($this->offsetExists($offset)) ? new TeamSpeak3_Helper_Char($this->string{$offset}) : null;
  }
TeamSpeak3_Helper_String::offsetSet ( offset,
value 
)

Definition at line 687 of file String.php.

References offsetExists().

  {
    if(!$this->offsetExists($offset)) return;

    $this->string{$offset} = strval($value);
  }

Definition at line 697 of file String.php.

References offsetExists().

  {
    if(!$this->offsetExists($offset)) return;

    $this->string = substr_replace($this->string, "", $offset, 1);
  }

Member Data Documentation

TeamSpeak3_Helper_String::$position = 0 [protected]

Definition at line 44 of file String.php.


The documentation for this class was generated from the following file:
 All Classes Files Functions Variables