\Horde_String

Provides static methods for charset and locale safe string manipulation.

Copyright 2003-2017 Horde LLC (http://www.horde.org/)

See the enclosed file LICENSE for license information (LGPL). If you did not receive this file, see http://www.horde.org/licenses/lgpl21.

Summary

Methods
Properties
Constants
convertCharset()
lower()
upper()
ucfirst()
ucwords()
substr()
length()
pos()
ipos()
rpos()
ripos()
pad()
wordwrap()
wrap()
truncate()
abbreviate()
common()
isAlpha()
isLower()
isUpper()
regexMatch()
validUtf8()
trimUtf8Bom()
No public properties found
No constants found
_convertCharset()
_pos()
_mbstringCharset()
$_lowers
$_uppers
N/A
No private methods found
No private properties found
N/A

Properties

$_lowers

$_lowers : array

lower() cache.

Type

array

$_uppers

$_uppers : array

upper() cache.

Type

array

Methods

convertCharset()

convertCharset(mixed  $input, string  $from, string  $to, boolean  $force = false) : mixed

Converts a string from one charset to another.

Uses the iconv or the mbstring extensions. The original string is returned if conversion failed or none of the extensions were available.

Parameters

mixed $input

The data to be converted. If $input is an an array, the array's values get converted recursively.

string $from

The string's current charset.

string $to

The charset to convert the string to.

boolean $force

Force conversion?

Returns

mixed —

The converted input data.

lower()

lower(string  $string, boolean  $locale = false, string  $charset = null) : string

Makes a string lowercase.

Parameters

string $string

The string to be converted.

boolean $locale

If true the string will be converted based on a given charset, locale independent else.

string $charset

If $locale is true, the charset to use when converting.

Returns

string —

The string with lowercase characters.

upper()

upper(string  $string, boolean  $locale = false, string  $charset = null) : string

Makes a string uppercase.

Parameters

string $string

The string to be converted.

boolean $locale

If true the string will be converted based on a given charset, locale independent else.

string $charset

If $locale is true, the charset to use when converting. If not provided the current charset.

Returns

string —

The string with uppercase characters.

ucfirst()

ucfirst(string  $string, boolean  $locale = false, string  $charset = null) : string

Returns a string with the first letter capitalized if it is alphabetic.

Parameters

string $string

The string to be capitalized.

boolean $locale

If true the string will be converted based on a given charset, locale independent else.

string $charset

The charset to use, defaults to current charset.

Returns

string —

The capitalized string.

ucwords()

ucwords(string  $string, boolean  $locale = false, string  $charset = null) : string

Returns a string with the first letter of each word capitalized if it is alphabetic.

Sentences are splitted into words at whitestrings.

Parameters

string $string

The string to be capitalized.

boolean $locale

If true the string will be converted based on a given charset, locale independent else.

string $charset

The charset to use, defaults to current charset.

Returns

string —

The capitalized string.

substr()

substr(string  $string, integer  $start, integer  $length = null, string  $charset = 'UTF-8') : string

Returns part of a string.

Parameters

string $string

The string to be converted.

integer $start

The part's start position, zero based.

integer $length

The part's length.

string $charset

The charset to use when calculating the part's position and length, defaults to current charset.

Returns

string —

The string's part.

length()

length(string  $string, string  $charset = 'UTF-8') : integer

Returns the character (not byte) length of a string.

Parameters

string $string

The string to return the length of.

string $charset

The charset to use when calculating the string's length.

Returns

integer —

The string's length.

pos()

pos(string  $haystack, string  $needle, integer  $offset, string  $charset = 'UTF-8') : integer

Returns the numeric position of the first occurrence of $needle in the $haystack string.

Parameters

string $haystack

The string to search through.

string $needle

The string to search for.

integer $offset

Character in $haystack to start searching at.

string $charset

Charset of $needle.

Returns

integer —

The position of first occurrence.

ipos()

ipos(string  $haystack, string  $needle, integer  $offset, string  $charset = 'UTF-8') : integer

Returns the numeric position of the first case-insensitive occurrence of $needle in the $haystack string.

Parameters

string $haystack

The string to search through.

string $needle

The string to search for.

integer $offset

Character in $haystack to start searching at.

string $charset

Charset of $needle.

Returns

integer —

The position of first case-insensitive occurrence.

rpos()

rpos(string  $haystack, string  $needle, integer  $offset, string  $charset = 'UTF-8') : integer

Returns the numeric position of the last occurrence of $needle in the $haystack string.

Parameters

string $haystack

The string to search through.

string $needle

The string to search for.

integer $offset

Character in $haystack to start searching at.

string $charset

Charset of $needle.

Returns

integer —

The position of last occurrence.

ripos()

ripos(string  $haystack, string  $needle, integer  $offset, string  $charset = 'UTF-8') : integer

Returns the numeric position of the last case-insensitive occurrence of $needle in the $haystack string.

Parameters

string $haystack

The string to search through.

string $needle

The string to search for.

integer $offset

Character in $haystack to start searching at.

string $charset

Charset of $needle.

Returns

integer —

The position of last case-insensitive occurrence.

pad()

pad(string  $input, integer  $length, string  $pad = ' ', \const  $type = STR_PAD_RIGHT, string  $charset = 'UTF-8') : string

Returns a string padded to a certain length with another string.

This method behaves exactly like str_pad() but is multibyte safe.

Parameters

string $input

The string to be padded.

integer $length

The length of the resulting string.

string $pad

The string to pad the input string with. Must be in the same charset like the input string.

\const $type

The padding type. One of STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH.

string $charset

The charset of the input and the padding strings.

Returns

string —

The padded string.

wordwrap()

wordwrap(string  $string, integer  $width = 75, string  $break = "\n", boolean  $cut = false, boolean  $line_folding = false) : string

Wraps the text of a message.

Parameters

string $string

String containing the text to wrap.

integer $width

Wrap the string at this number of characters.

string $break

Character(s) to use when breaking lines.

boolean $cut

Whether to cut inside words if a line can't be wrapped.

boolean $line_folding

Whether to apply line folding rules per RFC 822 or similar. The correct break characters including leading whitespace have to be specified too.

Returns

string —

String containing the wrapped text.

wrap()

wrap(string  $text, integer  $length = 80, string  $break_char = "\n", boolean  $quote = false) : string

Wraps the text of a message.

Parameters

string $text

String containing the text to wrap.

integer $length

Wrap $text at this number of characters.

string $break_char

Character(s) to use when breaking lines.

boolean $quote

Ignore lines that are wrapped with the '>' character (RFC 2646)? If true, we don't remove any padding whitespace at the end of the string.

Returns

string —

String containing the wrapped text.

truncate()

truncate(string  $text, integer  $length = 100) : string

Return a truncated string, suitable for notifications.

Parameters

string $text

The original string.

integer $length

The maximum length.

Returns

string —

The truncated string, if longer than $length.

abbreviate()

abbreviate(string  $text, integer  $length = 20) : string

Return an abbreviated string, with characters in the middle of the excessively long string replaced by '.

..'.

Parameters

string $text

The original string.

integer $length

The length at which to abbreviate.

Returns

string —

The abbreviated string, if longer than $length.

common()

common(string  $str1, string  $str2) : string

Returns the common leading part of two strings.

Parameters

string $str1

A string.

string $str2

Another string.

Returns

string —

The start of $str1 and $str2 that is identical in both.

isAlpha()

isAlpha(string  $string, string  $charset) : boolean

Returns true if the every character in the parameter is an alphabetic character.

Parameters

string $string

The string to test.

string $charset

The charset to use when testing the string.

Returns

boolean —

True if the parameter was alphabetic only.

isLower()

isLower(string  $string, string  $charset) : boolean

Returns true if ever character in the parameter is a lowercase letter in the current locale.

Parameters

string $string

The string to test.

string $charset

The charset to use when testing the string.

Returns

boolean —

True if the parameter was lowercase.

isUpper()

isUpper(string  $string, string  $charset) : boolean

Returns true if every character in the parameter is an uppercase letter in the current locale.

Parameters

string $string

The string to test.

string $charset

The charset to use when testing the string.

Returns

boolean —

True if the parameter was uppercase.

regexMatch()

regexMatch(string  $text, array  $regex, string  $charset = null) : array

Performs a multibyte safe regex match search on the text provided.

Parameters

string $text

The text to search.

array $regex

The regular expressions to use, without perl regex delimiters (e.g. '/' or '|').

string $charset

The character set of the text.

Returns

array —

The matches array from the first regex that matches.

validUtf8()

validUtf8(string  $text) : boolean

Check to see if a string is valid UTF-8.

Parameters

string $text

The text to check.

Returns

boolean —

True if valid UTF-8.

trimUtf8Bom()

trimUtf8Bom(string  $str) : string

Strip UTF-8 byte order mark (BOM) from string data.

Parameters

string $str

Input string (UTF-8).

Returns

string —

Stripped string (UTF-8).

_convertCharset()

_convertCharset(string  $input, string  $from, string  $to) : string

Internal function used to do charset conversion.

Parameters

string $input

See self::convertCharset().

string $from

See self::convertCharset().

string $to

See self::convertCharset().

Returns

string —

The converted string.

_pos()

_pos(string  $haystack, string  $needle, integer  $offset, string  $charset, string  $func) : integer

Perform string position searches.

Parameters

string $haystack

The string to search through.

string $needle

The string to search for.

integer $offset

Character in $haystack to start searching at.

string $charset

Charset of $needle.

string $func

Function to use.

Returns

integer —

The position of occurrence.

_mbstringCharset()

_mbstringCharset(string  $charset) : string

Workaround charsets that don't work with mbstring functions.

Parameters

string $charset

The original charset.

Returns

string —

The charset to use with mbstring functions.