Class StringUtils

Description

Collection of utility methods to handle with strings

Basically, this collection of static methods wraps the functions provided by PHP's standard string functions, and adds some new funcionalities.

Located in /core/text/StringUtils.class.php (line 45)

PHP2Go
   |
   --StringUtils
Method Summary
static string addLineNumbers ( &$str, [int $start = 1], [int $indent = 3], [string $afterNumberChar = ':'], [string $glue = "\n"], string $str)
static string allTrim (string $str)
static string camelize (string $str)
static string capitalize (string $str)
static string charAt (string $str, int $index)
static string concat (string $str, string $concat)
static int countChars (string $str, [bool $includeSpaces = FALSE])
static int countParagraphs (string $str)
static int countSentences (string $str)
static int countWords (string $str)
static string cutBefore (string $string, string $token, [bool $caseSensitive = TRUE])
static string cutLastOcurrence (string $string, string $cutOff, [bool $caseSensitive = TRUE])
static string decode (string $str, string $encodeType)
static string encode (string $str, string $encodeType, [array $params = NULL])
static bool endsWith (string $str, string $slice, [bool $caseSensitive = TRUE], [bool $ignSpaces = TRUE])
static string escape (string $str, [string $escapeType = 'html'])
static array explode (string $str, string $sep)
static string filter (string $str, [string $filterType = 'alphanum'], [string $replaceStr = ''])
static string ifEmpty (string $str, string $replacement)
static string implode (array $values, string $glue)
static string indent (string $str, int $nChars, [string $iChar = ' '])
static string insert (string $str, [string $insValue = ''], [int $insPos = 0])
static string insertChar (string $str, [string $char = ' '], [bool $stripEmpty = TRUE])
static bool isAllLower (string $str)
static bool isAllUpper (string $str)
static bool isEmpty (string $str)
static string left (string $str, [int $n = 0])
static mixed map ()
static bool match (string $str, string $search, [bool $caseSensitive = TRUE])
static string mid (string $str, [int $startAt = 1], [int $chars = 0])
static string normalize (string $str)
static string randomString (int $size, [bool $upper = TRUE], [bool $digit = TRUE])
static string regexReplace (string $str, string $pattern, string $replacement)
static string replace (string $str, string $from, string $to)
static string right (string $str, [int $n = 0])
static bool startsWith (string $str, string $slice, [bool $caseSensitive = TRUE], [bool $ignSpaces = TRUE])
static string stripBlank (string $str, [string $replace = ' '])
static string surround (string $str, string $prefix, string $suffix)
static string truncate (string $str, int $length, [int $truncSufix = '...'], [bool $forceBreak = TRUE])
static string wrap (string $str, int $num, [string $breakString = "\n"])
static string wrapLine (string $str, int $num, [string $breakString = "\n"])
Methods
static method addLineNumbers (line 777)

Adds line numbers on a given text

static string addLineNumbers ( &$str, [int $start = 1], [int $indent = 3], [string $afterNumberChar = ':'], [string $glue = "\n"], string $str)
  • string $str: Input text
  • int $start: Start numeration
  • int $indent: Start indentation
  • string $afterNumberChar: Suffix for the line numbers
  • string $glue: Line glue
  • &$str
static method allTrim (line 54)

Removes whitespace chars from left, right and inside a string

static string allTrim (string $str)
  • string $str: Input string
static method camelize (line 565)

Converts a string to camel case format

static string camelize (string $str)
  • string $str: Input string
static method capitalize (line 576)

Capitalizes all words of a given string

static string capitalize (string $str)
  • string $str: Input string
static method charAt (line 137)

Reads a char from a string

static string charAt (string $str, int $index)
  • string $str: Input string
  • int $index: Char index
static method concat (line 265)

Appends a value in the end of a string

static string concat (string $str, string $concat)
  • string $str: Input string
  • string $concat: Concat string
static method countChars (line 795)

Counts characters of a given string

static int countChars (string $str, [bool $includeSpaces = FALSE])
  • string $str: Input string
  • bool $includeSpaces: Consider whitespace chars when couting
static method countParagraphs (line 834)

Counts the number of paragraphs of a given text

static int countParagraphs (string $str)
  • string $str: Input text
static method countSentences (line 822)

Counts the number of sentences of a given text

static int countSentences (string $str)
  • string $str: Input text
static method countWords (line 811)

Counts the number of words of a given text

static int countWords (string $str)
  • string $str: Input text
static method cutBefore (line 621)

Remove all chars in a string before a given token is found

  1.  /* prints "order by name" */
  2.  print StringUtils::cutBefore("select * from table order by name""order by");

static string cutBefore (string $string, string $token, [bool $caseSensitive = TRUE])
  • string $string: Input string
  • string $token: Token
  • bool $caseSensitive: Case sensitive?
static method cutLastOcurrence (line 641)

Removes all chars after the last ocurrence of $cutOff in $string

  1.  /* prints "select * from table" */
  2.  print StringUtils::cutLastOcurrence("select * from table where active = 1""where");

static string cutLastOcurrence (string $string, string $cutOff, [bool $caseSensitive = TRUE])
  • string $string: Input string
  • string $cutOff: Token
  • bool $caseSensitive: Case sensitive?
static method decode (line 419)

Decodes a given string

Supported encoding types: base64, utf8 and quoted-printable.

static string decode (string $str, string $encodeType)
  • string $str: Input string
  • string $encodeType: Encoding type
static method encode (line 377)

Encodes a given string

The supported encoding types (and their parameters) are base64 (none), utf8 (none), 7bit (nl), 8bit (nl) and quoted-printable (charset).

Examples:

  1.  StringUtils::encode('encode me''base64');
  2.  StringUtils::encode('encode me''7bit');
  3.  StringUtils::encode('quoted printable''quoted-printable'array('charset' => 'utf-8'));

static string encode (string $str, string $encodeType, [array $params = NULL])
  • string $str: Input string
  • string $encodeType: Encode type
  • array $params: Encoding params
static method endsWith (line 196)

Checks if a given string ends with a given value

static bool endsWith (string $str, string $slice, [bool $caseSensitive = TRUE], [bool $ignSpaces = TRUE])
  • string $str: Input string
  • string $slice: Comparison value
  • bool $caseSensitive: Case sensitive?
  • bool $ignSpaces: Ignore initial whitespace chars
static method escape (line 528)

Escapes a string according to a given pattern

Patterns:

  • html: escapes HTML special chars
  • htmlall: escapes HTML entities
  • url: escapes URL special chars (using http://www.php.net/rawurlencode)
  • quotes: escapes quotes
  • javascript: escapes JS code
  • mail: replaces '@' by 'at' and '.' by 'dot'

static string escape (string $str, [string $escapeType = 'html'])
  • string $str: Input string
  • string $escapeType
static method explode (line 340)

Splits the string using $sep as separator

  • return: Resultant array
static array explode (string $str, string $sep)
  • string $str: Input string
  • string $sep: Separator
static method filter (line 490)

Applies a given filter on a string

Filter types:

  • alpha: removes all alpha chars
  • alphalower : removes all lowercase alpha chars
  • alphaupper : removes all uppercase alpha chars
  • num : removes all numbers
  • alphanum : removes all alphanumeric chars
  • htmlentities : removes all html entities
  • blank : removes all whitespace chars

static string filter (string $str, [string $filterType = 'alphanum'], [string $replaceStr = ''])
  • string $str: Input string
  • string $filterType: Filter type
  • string $replaceStr: Replacement string
static method ifEmpty (line 253)

Returns a fallback value when a given string is empty

static string ifEmpty (string $str, string $replacement)
  • string $str: Input string
  • string $replacement: Fallback string
static method implode (line 353)

Implodes an array, returning the result as a string

static string implode (array $values, string $glue)
  • array $values: Input array
  • string $glue: Glue
static method indent (line 656)

Indents a given text using $iChar as indent char

static string indent (string $str, int $nChars, [string $iChar = ' '])
  • string $str: Input string
  • int $nChars: Number of times to repeat the indent char
  • string $iChar: Indent char
static method insert (line 291)

Inserts a value in a given position of a string

static string insert (string $str, [string $insValue = ''], [int $insPos = 0])
  • string $str: Input string
  • string $insValue: Insert value
  • int $insPos: Insert position
static method insertChar (line 700)

Insert a char between every pair of chars of a string

static string insertChar (string $str, [string $char = ' '], [bool $stripEmpty = TRUE])
  • string $str: Input string
  • string $char: Char to be inserted
  • bool $stripEmpty: Strip whitespace chars
static method isAllLower (line 225)

Checks if a string is composed only by lowercase chars

static bool isAllLower (string $str)
  • string $str: Input string
static method isAllUpper (line 214)

Checks if a string is composed only by uppercase chars

static bool isAllUpper (string $str)
  • string $str: Input string
static method isEmpty (line 240)

Safely checks if a string is empty

A string will be considered empty when its length is 0 and a call to the empty() function returns TRUE.

static bool isEmpty (string $str)
  • string $str: Input string
static method left (line 78)

Get $n chars from the left side of a string

static string left (string $str, [int $n = 0])
  • string $str: Input string
  • int $n: Number of chars
static method map (line 455)

This method has the functionality of an if-then-else statement

Given the function arguments, the method analyzes them in pairs, starting from the second (the first is the input string). For each pair A and B, if A is equal to the original string, B is returned. If none of the pairs matches, the last argument can be used as an "else".

Examples:

  1.  $result StringUtils::map($value1'yes'0'no''unknown');
  2.  $result StringUtils::map($booltrue'true'false'false');

static mixed map ()
static method match (line 155)

Checks if a value is present in a given string

static bool match (string $str, string $search, [bool $caseSensitive = TRUE])
  • string $str: Input string
  • string $search: Search value
  • bool $caseSensitive: Whether to do case sensitive search
static method mid (line 118)

Reads a portion of a string, start at $startAt

The start index is 1-based. Example:

  1.  $sb new String Buffer('hello world');
  2.  /* prints "hello" */
  3.  print $sb->mid(15);

static string mid (string $str, [int $startAt = 1], [int $chars = 0])
  • string $str: Input string
  • int $startAt: Start index
  • int $chars: Substring length
static method normalize (line 601)

Normalizes a given string

Replaces all chars from positions 192-223 and 224-225 of the ASCII table by their 'normal' versions: áéíÁÈÒÖ by aeiAEOO.

static string normalize (string $str)
  • string $str: Input string
static method randomString (line 847)

Generates a random string

static string randomString (int $size, [bool $upper = TRUE], [bool $digit = TRUE])
  • int $size: Desired string length
  • bool $upper: Whether to use uppercase chars
  • bool $digit: Whether to use digits
static method regexReplace (line 323)

Performs a regular expression search and replace on a string

static string regexReplace (string $str, string $pattern, string $replacement)
  • string $str: Input string
  • string $pattern: PCRE pattern
  • string $replacement: Replacement
static method replace (line 310)

Replaces all occurrences of $from by $to in a given string

static string replace (string $str, string $from, string $to)
  • string $str: Input string
  • string $from: Search
  • string $to: Replace
static method right (line 94)

Get $n chars from the right side of a string

static string right (string $str, [int $n = 0])
  • string $str: Input string
  • int $n: Number of chars
static method startsWith (line 175)

Checks if a given string starts with a given value

static bool startsWith (string $str, string $slice, [bool $caseSensitive = TRUE], [bool $ignSpaces = TRUE])
  • string $str: Input string
  • string $slice: Comparison value
  • bool $caseSensitive: Case sensitive?
  • bool $ignSpaces: Ignore initial whitespace chars
static method stripBlank (line 66)

Replaces 2 or more whitespace chars in a string

static string stripBlank (string $str, [string $replace = ' '])
  • string $str: Input string
  • string $replace: Replacement string
static method surround (line 278)

Surrounds a string with a prefix and a suffix

static string surround (string $str, string $prefix, string $suffix)
  • string $str: Input string
  • string $prefix: Prefix
  • string $suffix: Suffix
static method truncate (line 676)

Truncate a given string to $length

If the length of the original string is lower than $length, the original string is returned.

static string truncate (string $str, int $length, [int $truncSufix = '...'], [bool $forceBreak = TRUE])
  • string $str: Input string
  • int $length: New length
  • int $truncSufix: Suffix to be appended in the end of the truncated string
  • bool $forceBreak: Force break of long words
static method wrap (line 752)

Adds or adjusts line breaks on a string, using $num chars per line

The $breakString parameter defines the character(s) to be used to represent a line break.

static string wrap (string $str, int $num, [string $breakString = "\n"])
  • string $str: Input string
  • int $num: Chars per line
  • string $breakString: Line break string
static method wrapLine (line 722)

Adds line wraps on a given string

The number of chars per line is defined by the $num parameter. The $breakString parameter defines the character(s) to be used to represent a line break.

static string wrapLine (string $str, int $num, [string $breakString = "\n"])
  • string $str: Input string
  • int $num: Chars per line
  • string $breakString: Line break string

Inherited Methods

Inherited From PHP2Go

PHP2Go::PHP2Go()
PHP2Go::equals()
PHP2Go::generateUniqueId()
PHP2Go::getClassName()
PHP2Go::getConfigVal()
PHP2Go::getLangVal()
PHP2Go::getObjectName()
PHP2Go::getParentName()
PHP2Go::hasDestructor()
PHP2Go::hashCode()
PHP2Go::isA()
PHP2Go::isSubclassOf()
PHP2Go::logError()
PHP2Go::raiseError()
PHP2Go::registerDestructor()
PHP2Go::registerShutdownFunc()
PHP2Go::retrieve()
PHP2Go::store()
PHP2Go::__toString()

Documentation generated on Sun, 07 Oct 2007 19:29:54 -0300 by phpDocumentor 1.3.2