String handling with encoding support

PHP needs to be compiled with --enable-mbstring or a fallback without encoding support is used

 Methods

Returns a closure that will alternate between the args which to return.

alternator() : \Fuel\Core\Closure

If you call the closure with false as the arg it will return the value without alternating the next time.

Returns

\Fuel\Core\Closure

Checks whether a string has a precific ending.

ends_with(string $str, string $end, boolean $ignore_case) : boolean

Parameters

$str

string

string to check

$end

string

ending to check for

$ignore_case

boolean

whether to ignore the case

Returns

booleanwhether a string ends with a specified ending

Add's _1 to a string or increment the ending number to allow _2, _3, etc

increment(string $str, int $first, string $separator) : string

Parameters

$str

string

required

$first

int

number that is used to mean first

$separator

string

separtor between the name and the number

Returns

string

Check if a string is html

is_html(string $string) : bool

Parameters

$string

string

string to check

Returns

bool

Check if a string is json encoded

is_json(string $string) : bool

Parameters

$string

string

string to check

Returns

bool

Check if a string is serialized

is_serialized(string $string) : bool

Parameters

$string

string

string to check

Returns

bool

Check if a string is a valid XML

is_xml(string $string) : bool

Parameters

$string

string

string to check

Exceptions

\FuelException

Returns

bool

lcfirst

lcfirst(string $str, string $encoding) : string

Does not strtoupper first

Parameters

$str

string

required

$encoding

string

default UTF-8

Returns

string

length()

length($str, $encoding) 

Parameters

$str

$encoding

lower()

lower($str, $encoding) 

Parameters

$str

$encoding

Creates a random string of characters

random(string $type, int $length) : string

Parameters

$type

string

the type of string

$length

int

the number of characters

Returns

stringthe random string

Checks whether a string has a precific beginning.

starts_with(string $str, string $start, boolean $ignore_case) : boolean

Parameters

$str

string

string to check

$start

string

beginning to check for

$ignore_case

boolean

whether to ignore the case

Returns

booleanwhether a string starts with a specified beginning

stripos — Find the position of the first occurrence of a case-insensitive substring in a string

stripos(string $haystack, mixed $needle, int $offset, string $encoding) : mixed

Parameters

$haystack

string

The string from which to get the position of the last occurrence of needle

$needle

mixed

The string to find in haystack

$offset

int

The search offset

$encoding

string

Defaults to the setting in the config, which defaults to UTF-8

Returns

mixedReturns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1. Returns FALSE if the needle was not found.

stristr — Finds first occurrence of a string within another, case-insensitive

stristr(string $haystack, mixed $needle, int $before_needle, string $encoding) : mixed

Parameters

$haystack

string

The string from which to get the position of the last occurrence of needle

$needle

mixed

The string to find in haystack

$before_needle

int

Determines which portion of haystack this function returns

$encoding

string

Defaults to the setting in the config, which defaults to UTF-8

Returns

mixedThe portion of haystack, or FALSE if needle is not found

strpos — Find the position of the first occurrence of a substring in a string

strlen(string $str, string $encoding) : int

Parameters

$str

string

The string being measured for length.

$encoding

string

Defaults to the setting in the config, which defaults to UTF-8

Returns

intThe length of the string on success, and 0 if the string is empty.

strpos — Find position of first occurrence of string in a string

strpos(string $haystack, mixed $needle, int $offset, string $encoding) : mixed

Parameters

$haystack

string

The string being checked

$needle

mixed

The string to find in haystack

$offset

int

The search offset

$encoding

string

Defaults to the setting in the config, which defaults to UTF-8

Returns

mixedReturns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1. Returns FALSE if the needle was not found.

strrchr — Finds the last occurrence of a character in a string within another

strrchr(string $haystack, mixed $needle, $before_needle, string $encoding) : mixed

Parameters

$haystack

string

The string from which to get the last occurrence of needle

$needle

mixed

The string to find in haystack

$before_needle

$encoding

string

Defaults to the setting in the config, which defaults to UTF-8

Returns

mixedThe portion of haystack, or FALSE if needle is not found

strripos — Finds position of last occurrence of a string within another, case insensitive

strripos(string $haystack, mixed $needle, int $offset, string $encoding) : mixed

Parameters

$haystack

string

The string from which to get the position of the last occurrence of needle

$needle

mixed

The string to find in haystack

$offset

int

The search offset

$encoding

string

Defaults to the setting in the config, which defaults to UTF-8

Returns

mixedReturns the numeric position of the last occurrence of needle in the haystack string. If needle is not found, it returns FALSE.

strrpos — Find position of last occurrence of a string in a string

strrpos(string $haystack, mixed $needle, int $offset, string $encoding) : mixed

Parameters

$haystack

string

The string being checked

$needle

mixed

The string to find in haystack

$offset

int

The search offset

$encoding

string

Defaults to the setting in the config, which defaults to UTF-8

Returns

mixedReturns the numeric position of the last occurrence of needle in the haystack string. If needle is not found, it returns FALSE.

strstr — Finds first occurrence of a string within another

strstr(string $haystack, mixed $needle, int $before_needle, string $encoding) : mixed

Parameters

$haystack

string

The string from which to get the position of the last occurrence of needle

$needle

mixed

The string to find in haystack

$before_needle

int

Determines which portion of haystack this function returns

$encoding

string

Defaults to the setting in the config, which defaults to UTF-8

Returns

mixedThe portion of haystack, or FALSE if needle is not found

strtolower — Make a string lowercase

strtolower(string $str, string $encoding) : string

Parameters

$str

string

The string to convert to lowercase

$encoding

string

Defaults to the setting in the config, which defaults to UTF-8

Returns

stringThe lowercased string

strtoupper — Make a string uppercase

strtoupper(string $str, string $encoding) : string

Parameters

$str

string

The string to convert to uppercase

$encoding

string

Defaults to the setting in the config, which defaults to UTF-8

Returns

stringThe uppercased string

sub()

sub($str, $start, $length, $encoding) 

Parameters

$str

$start

$length

$encoding

substr()

substr($str, $start, $length, $encoding) 

Parameters

$str

$start

$length

$encoding

substr_count — Count the number of substring occurrences

substr_count(string $haystack, mixed $needle, int $offset, string $encoding) : int

Parameters

$haystack

string

The string from which to get the position of the last occurrence of needle

$needle

mixed

The string to find in haystack

$offset

int

The search offset

$encoding

string

Defaults to the setting in the config, which defaults to UTF-8

Returns

intThe number of occurences found

Parse the params from a string using strtr()

tr(string $string, array $array) : string

Parameters

$string

string

string to parse

$array

array

params to str_replace

Returns

string

Truncates a string to the given length.

truncate(string $string, int $limit, string $continuation, bool $is_html) : string

It will optionally preserve HTML tags if $is_html is set to true.

Parameters

$string

string

the string to truncate

$limit

int

the number of characters to truncate too

$continuation

string

the string to use to denote it was truncated

$is_html

bool

whether the string has HTML

Returns

stringthe truncated string

ucfirst

ucfirst(string $str, string $encoding) : string

Does not strtolower first

Parameters

$str

string

required

$encoding

string

default UTF-8

Returns

string

ucwords

ucwords(string $str, string $encoding) : string

First strtolower then ucwords

ucwords normally doesn't strtolower first but MB_CASE_TITLE does, so ucwords now too

Parameters

$str

string

required

$encoding

string

default UTF-8

Returns

string

upper()

upper($str, $encoding) 

Parameters

$str

$encoding