Class kartik\base\Lib

Inheritancekartik\base\Lib
Available since version1.0

Lib is a base class for modified standard PHP internal functions. It is specifically built to address warnings in PHP v8.1 and above due to null arguments passed to PHP internal functions which results in deprecation errors in PHP v8.1 and beyond.

Usage:

use kartik\helpers\Lib;

// examples of usage
echo Lib::trim(' String ');

Public Methods

Hide inherited methods

MethodDescriptionDefined By
explode() Split a string by a string. kartik\base\Lib
html_entity_decode() Convert HTML entities to their corresponding characters. kartik\base\Lib
lcfirst() Make a string's first character lowercase kartik\base\Lib
nl2br() Inserts HTML line breaks before all newlines in a string kartik\base\Lib
preg_filter() Perform a regular expression search and replace. kartik\base\Lib
preg_match() Perform a regular expression match kartik\base\Lib
preg_match_all() Perform a global regular expression match. kartik\base\Lib
preg_replace() Perform a regular expression search and replace. kartik\base\Lib
preg_replace_callback() Perform a regular expression search and replace using a callback. kartik\base\Lib
preg_replace_callback_array() Perform a regular expression search and replace using callbacks. kartik\base\Lib
preg_split() Split string by a regular expression. kartik\base\Lib
rawurldecode() Decode URL-encoded strings kartik\base\Lib
rawurlencode() URL-encode according to RFC 3986 kartik\base\Lib
str_ireplace() Case-insensitive version of str_replace(). kartik\base\Lib
str_repeat() Repeat a string. kartik\base\Lib
str_replace() Replace all occurrences of the search string with the replacement string. kartik\base\Lib
strip_tags() Strip HTML and PHP tags from a string kartik\base\Lib
stripos() Find position of last occurrence of a case-insensitive string in a string kartik\base\Lib
strlen() Get string length. kartik\base\Lib
strncmp() Binary safe string comparison of the first n characters kartik\base\Lib
strpos() Find the position of the first occurrence of a substring in a string. kartik\base\Lib
strrpos() Find the position of the last occurrence of a substring in a string kartik\base\Lib
strtolower() Make a string lowercase. kartik\base\Lib
strtoupper() Make a string uppercase. kartik\base\Lib
strtr() Translate certain characters. kartik\base\Lib
substr() Return part of a string. kartik\base\Lib
trim() Strip whitespace (or other characters) from the beginning and end of a string. kartik\base\Lib
ucfirst() Make a string's first character uppercase kartik\base\Lib
ucwords() Uppercase the first character of each word in a string kartik\base\Lib
urldecode() Decodes URL-encoded string kartik\base\Lib
urlencode() URL-encodes string. kartik\base\Lib

Method Details

explode() public static method

Split a string by a string.

public static string[]|false explode ( $separator, $string, $limit null )
$separator string

The boundary string.

$string string

The input string.

$limit integer|null

If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string. If the limit parameter is negative, all components except the last -limit are returned. If the limit parameter is zero, then this is treated as 1.

return string[]|false

If delimiter is an empty string (""), explode will return false. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned. For any other limit, an array containing string will be returned.

html_entity_decode() public static method

Convert HTML entities to their corresponding characters.

public static string html_entity_decode ( $string, $flags ENT_COMPAT, $encoding null )
$string string

The input string.

$flags integer

The optional second quote_style parameter lets you define what will be done with 'single' and "double" quotes. It takes on one of three constants with the default being ENT_COMPAT . Available quote_style constants:

  • ENT_COMPAT: Will convert double-quotes and leave single-quotes alone.
  • ENT_QUOTES: Will convert both double and single quotes.
  • ENT_NOQUOTES: Will leave both double and single quotes unconverted.
$encoding string|null

The ISO-8859-1 character set is used as default for the optional third charset. This defines the character set used in conversion.

return string

The decoded string.

lcfirst() public static method

Make a string's first character lowercase

public static string lcfirst ( $string )
$string string

The input string.

return string

The resulting string.

nl2br() public static method

Inserts HTML line breaks before all newlines in a string

public static string nl2br ( $string, $use_xhtml true )
$string string

The input string.

$use_xhtml boolean

Whenever to use XHTML compatible line breaks or not.

return string

The altered string.

preg_filter() public static method

Perform a regular expression search and replace.

public static string|string[]|null preg_filter ( $pattern, $replacement, $subject, $limit = -1, &$count null )
$pattern string|string[]

The pattern to search for. It can be either a string or an array with strings. Several PCRE modifiers are also available, including the deprecated e (PREG_REPLACE_EVAL), which is specific to this function.

$replacement string|string[]

The string or an array with strings to replace.

  • If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string.
  • If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart.
  • If there are fewer elements in the replacement array than in the pattern array, any extra patterns will be replaced by an empty string.
  • replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\\\" PHP string).
  • When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \\1 notation for your backreference. \\11, for example, would confuse preg_replace since it does not know whether you want the \\1 backreference followed by a literal 1, or the \\11 backreference followed by nothing. In this case the solution is to use \${1}1. This creates an isolated $1 backreference, leaving the 1 as a literal.
  • When using the deprecated e modifier, this function escapes some characters (namely ', ", \ and NULL) in the strings that replace the backreferences. This is done to ensure that no syntax errors arise from backreference usage with either single or double quotes (e.g. strlen(\'$1\') + strlen("$2")). Make sure you are aware of PHP's string syntax to know exactly how the interpreted string will look.
$subject string|string[]

The string or an array with strings to search and replace. If subject is an array, then the search and replace is performed on every entry of subject , and the return value is an array as well.

$limit integer

The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

$count integer

If specified, this variable will be filled with the number of replacements done.

return string|string[]|null

An array if the subject parameter is an array, or a string otherwise. If no matches are found or an error occurred, an empty array is returned when subject is an array or NULL otherwise.

preg_match() public static method

Perform a regular expression match

public static integer|false preg_match ( $pattern, $subject, &$matches, $flags 0, $offset 0 )
$pattern string

The pattern to search for, as a string.

$subject string

The input string.

$matches string[]

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

$flags integer

flags can be the following flags:

  • PREG_OFFSET_CAPTURE: If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
Lib::preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
print_r($matches);

The above example will output:

Array
(
  [0] => Array
    (
      [0] => foobarbaz
      [1] => 0
    )

  [1] => Array
    (
        [0] => foo
        [1] => 0
    )

  [2] => Array
    (
        [0] => bar
        [1] => 3
    )

  [3] => Array
    (
        [0] => baz
        [1] => 6
    )
)
  • PREG_UNMATCHED_AS_NULL : If this flag is passed, unmatched subpatterns are reported as NULL; otherwise they are reported as an empty string.
Lib::preg_match('/(a)(b)*(c)/', 'ac', $matches);
var_dump($matches);
Lib::preg_match('/(a)(b)*(c)/', 'ac', $matches, PREG_UNMATCHED_AS_NULL);
var_dump($matches);

The above example will output:

array(4) {
  [0]=> string(2) "ac"
  [1]=> string(1) "a"
  [2]=> string(0) ""
  [3]=> string(1) "c"
}
array(4) {
  [0]=> string(2) "ac"
  [1]=> string(1) "a"
  [2]=> NULL
  [3]=> string(1) "c"
}
$offset integer

Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes). Using offset is not equivalent to passing substr($subject, $offset) to preg_match in place of the subject string, because pattern can contain assertions such as ^, $ or (?<=x).

Compare:

$subject = "abcdef";
$pattern = '/^def/';
Lib::preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);

The above example will output:

Array
(
)

while this example

$subject = "abcdef";
$pattern = '/^def/';
Lib::preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);

will produce:

Array
(
[0] => Array
    (
        [0] => def
        [1] => 0
    )
)

Alternatively, to avoid using substr(), use the \G assertion rather than the ^ anchor, or the A modifier instead, both of which work with the offset parameter.

return integer|false

preg_match returns 1 if the pattern matches given subject , 0 if it does not, or FALSE if an error occurred.

preg_match_all() public static method

Perform a global regular expression match.

public static integer|false|null preg_match_all ( $pattern, $subject, &$matches, $flags PREG_PATTERN_ORDER, $offset 0 )
$pattern string

The pattern to search for, as a string.

$subject string

The input string.

$matches string[][]

Array of all matches in multi-dimensional array ordered according to flags.

$flags integer

Can be a combination of the following flags (note that it doesn't make sense to use PREG_PATTERN_ORDER together with PREG_SET_ORDER):

  • PREG_PATTERN_ORDER: Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on. For example:
preg_match_all(
    "|<[^>]+>(.*)</[^>]+>|U",
    "<b>example: </b><div align=left>this is a test</div>",
    $out, PREG_PATTERN_ORDER
);
echo $out[0][0] . ", " . $out[0][1] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";

The above example will output:

<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test

So, $out[0] contains array of strings that matched full pattern, and $out[1] contains array of strings enclosed by tags.

If the pattern contains named subpatterns, $matches additionally contains entries for keys with the subpattern name.

If the pattern contains duplicate named subpatterns, only the rightmost subpattern is stored in $matches[NAME].

preg_match_all(
    '/(?J)(?<match>foo)|(?<match>bar)/',
    'foo bar',
    $matches,
    PREG_PATTERN_ORDER
);
print_r($matches['match'])

The above example will output:

Array
(
    [0] =>
    [1] => bar
)
  • PREG_SET_ORDER: Orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on. For example:
preg_match_all(
    "|<[^>]+>(.*)</[^>]+>|U",
    "<b>example: </b><div align=left>this is a test</div>",
    $out, PREG_SET_ORDER
);
echo $out[0][0] . ", " . $out[0][1] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";

The above example will output

<b>example: </b>, example:
<div align="left">this is a test</div>, this is a test
  • PREG_OFFSET_CAPTURE: If this flag is passed, for every occurring match the appendant string offset (in bytes) will also be returned. Note that this changes the value of matches into an array of arrays where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
preg_match_all('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
print_r($matches);

The above example will output

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => foobarbaz
                    [1] => 0
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => foo
                    [1] => 0
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [0] => bar
                    [1] => 3
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [0] => baz
                    [1] => 6
                )

        )

)
  • PREG_UNMATCHED_AS_NULL: If this flag is passed, unmatched subpatterns are reported as NULL; otherwise they are reported as an empty string.

If no order flag is given, PREG_PATTERN_ORDER is assumed.

$offset integer

Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes). Using offset is not equivalent to passing substr($subject, $offset) to preg_match_all in place of the subject string, because pattern can contain assertions such as ^, $ or (?<=x). See preg_match() for examples.

Lib::preg_match_all("|]+>(.*)]+>|U", "example: this is a test", $out, PREG_PATTERN_ORDER);
echo $out[0][0] . ", " . $out[0][1] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";

The above example will output:

example: , this is a test
example: , this is a test

So, $out[0] contains array of strings that matched full pattern, and $out[1] contains array of strings enclosed by tags.

return integer|false|null

The number of full pattern matches (which might be zero), or FALSE if an error occurred.

preg_replace() public static method

Perform a regular expression search and replace.

public static string|string[]|null preg_replace ( $pattern, $replacement, $subject, $limit = -1, &$count null )
$pattern string|string[]

The pattern to search for. It can be either a string or an array with strings. Several PCRE modifiers are also available, including the deprecated 'e' (PREG_REPLACE_EVAL), which is specific to this function.

$replacement string|string[]

The string or an array with strings to replace.

  • If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string.
  • If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart.
  • If there are fewer elements in the replacement array than in the pattern array, any extra patterns will be replaced by an empty string.
  • replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\\\" PHP string).
  • When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \\1 notation for your backreference. \\11, for example, would confuse preg_replace since it does not know whether you want the \\1 backreference followed by a literal 1, or the \\11 backreference followed by nothing. In this case the solution is to use \${1}1. This creates an isolated $1 backreference, leaving the 1 as a literal.
  • When using the deprecated e modifier, this function escapes some characters (namely ', ", \ and NULL) in the strings that replace the backreferences. This is done to ensure that no syntax errors arise from backreference usage with either single or double quotes (e.g. strlen(\'$1\') + strlen("$2")). Make sure you are aware of PHP's string syntax to know exactly how the interpreted string will look.
$subject string|string[]

The string or an array with strings to search and replace. If subject is an array, then the search and replace is performed on every entry of subject , and the return value is an array as well.

$limit integer

The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

$count integer

If specified, this variable will be filled with the number of replacements done.

return string|string[]|null

preg_replace returns an array if the subject parameter is an array, or a string otherwise. If matches are found, the new subject will be returned, otherwise subject will be returned unchanged or NULL if an error occurred.

preg_replace_callback() public static method

Perform a regular expression search and replace using a callback.

public static string|string[]|null preg_replace_callback ( $pattern, $callback, $subject, $limit = -1, &$count null, $flags 0 )
$pattern string|string[]

The pattern to search for. It can be either a string or an array with strings. *

$callback callable

A callback that will be called and passed an array of matched elements in the subject string. The callback should return the replacement string. This is the callback signature:

handler(array $matches): string

You'll often need the callback function for a preg_replace_callback in just one place. In this case you can use an anonymous function to declare the callback within the call to preg_replace_callback. By doing it this way you have all information for the call in one place and do not clutter the function namespace with a callback function's name not used anywhere else.

Example of usage of preg_replace_callback and anonymous function:

// a unix-style command line filter to convert uppercase
// letters at the beginning of paragraphs to lowercase
$fp = fopen("php://stdin", "r") or die("can't read stdin");
while (!feof($fp)) {
    $line = fgets($fp);
    $line = Lib::preg_replace_callback( '|\s*\w|', function ($matches) {
         return Lib::strtolower($matches[0]);
    }, $line);
    echo $line;
}
fclose($fp);
$subject string|string[]

The string or an array with strings to search and replace.

$limit integer

The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

$count integer

If specified, this variable will be filled with the number of replacements done.

$flags integer

Can be a combination of the PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL flags, which influence the format of the matches array. See the description in preg_match() for more details.

return string|string[]|null

preg_replace_callback returns an array if the subject parameter is an array, or a string otherwise. On errors the return value is NULL If matches are found, the new subject will be returned, otherwise subject will be returned unchanged.

preg_replace_callback_array() public static method

Perform a regular expression search and replace using callbacks.

public static string|string[]|null preg_replace_callback_array ( $pattern, $subject, $limit = -1, &$count null, $flags 0 )
$pattern array|callable[]

An associative array mapping patterns (keys) to callbacks (values)

$subject string|string[]

The string or an array with strings to search and replace.

$limit integer

The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

$count integer

If specified, this variable will be filled with the number of replacements done.

$flags integer

Can be a combination of the PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL flags, which influence the format of the matches array. See the description in preg_match() for more details.

return string|string[]|null

preg_replace_callback_array() returns an array if the subject parameter is an array, or a string otherwise. On errors the return value is NULL. If matches are found, the new subject will be returned, otherwise subject will be returned unchanged.

preg_split() public static method

Split string by a regular expression.

public static string[]|false preg_split ( $pattern, $subject, $limit = -1, $flags 0 )
$pattern string

The pattern to search for, as a string.

$subject string

The input string.

$limit integer

If specified, then only substrings up to limit are returned with the rest of the string being placed in the last substring. A limit of -1, 0 or NULL means "no limit" and, as is standard across PHP, you can use NULL to skip to the flags parameter.

$flags integer

flags can be any combination of the following flags (combined with the | bitwise operator): PREG_SPLIT_NO_EMPTY If this flag is set, only non-empty pieces will be returned by preg_split.

return string[]|false

An array containing substrings of subject split along boundaries matched by pattern, or FALSE if an error occurred.

rawurldecode() public static method

Decode URL-encoded strings

public static string rawurldecode ( $string )
$string string

The URL to be encoded.

return string

The decoded URL, as a string.

rawurlencode() public static method

URL-encode according to RFC 3986

public static string rawurlencode ( $string )
$string string

The string to be encoded.

return string

A string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits. This is the encoding described in RFC 1738 for protecting literal characters from being interpreted as special URL delimiters, and for protecting URLs from being mangled by transmission media with character conversions (like some email systems).

str_ireplace() public static method

Case-insensitive version of str_replace().

public static string|string[] str_ireplace ( $search, $replace, $subject, &$count null )
$search string|string[]

The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.

$replace string|string[]

The replacement value that replaces found search values. An array may be used to designate multiple replacements.

$subject string|string[]

The string or array being searched and replaced on, otherwise known as the haystack. If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well.

$count integer

If passed, this will hold the number of matched and replaced needles.

return string|string[]

This function returns a string or an array with the replaced values.

str_repeat() public static method

Repeat a string.

public static string str_repeat ( $string, $times null )
$string string

The string to be repeated.

$times integer|null

Number of time the input string should be repeated. The multiplier has to be greater than or equal to 0. If the multiplier is set to 0, the function will return an empty string.

return string

The repeated string.

str_replace() public static method

Replace all occurrences of the search string with the replacement string.

public static string|string[] str_replace ( $search, $replace, $subject, &$count null )
$search string|string[]

The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.

$replace string|string[]

The replacement value that replaces found search values. An array may be used to designate multiple replacements.

$subject string|string[]

The string or array being searched and replaced on, otherwise known as the haystack. If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well.

$count integer

If passed, this will hold the number of matched and replaced needles.

return string|string[]

This function returns a string or an array with the replaced values.

strip_tags() public static method

Strip HTML and PHP tags from a string

public static string strip_tags ( $string, $allowed_tags null )
$string string

The input string.

$allowed_tags string[]|string|null

You can use the optional second parameter to specify tags which should not be stripped. HTML comments and PHP tags are also stripped. This is hardcoded and can not be changed with allowable_tags.

return string

The stripped string.

stripos() public static method

Find position of last occurrence of a case-insensitive string in a string

public static integer|false stripos ( $haystack, $needle, $offset 0 )
$haystack string

The string to search in

$needle string

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

$offset integer

If specified, search will start this number of characters counted from the beginning of the string. If the value is negative, search will instead start from that many characters from the end of the string, searching backwards.

return integer|false

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

strlen() public static method

Get string length.

public static integer strlen ( $string )
$string string

The string being measured for length.

return integer

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

strncmp() public static method

Binary safe string comparison of the first n characters

public static integer strncmp ( $string1, $string2, $length )
$string1 string

The first string.

$string2 string

The second string.

$length integer

Number of characters to use in the comparison.

return integer

< 0 if str1 is less than str2, > 0 if str1 is greater than str2, and = 0 if they are equal.

strpos() public static method

Find the position of the first occurrence of a substring in a string.

public static integer|false strpos ( $haystack, $needle, $offset 0 )
$haystack string

The string to search in

$needle string

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

$offset integer

If specified, search will start this number of characters counted from the beginning of the string. Unlike strrpos() and stripos(), the offset cannot be negative.

return integer|false

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

strrpos() public static method

Find the position of the last occurrence of a substring in a string

public static integer|false strrpos ( $haystack, $needle, $offset 0 )
$haystack string

The string to search in

$needle string

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

$offset integer

If specified, search will start this number of characters counted from the beginning of the string. If the value is negative, search will instead start from that many characters from the end of the string, searching backwards.

return integer|false

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

strtolower() public static method

Make a string lowercase.

public static string strtolower ( $string )
$string string|null

The input string.

return string

The lowercased string.

strtoupper() public static method

Make a string uppercase.

public static string strtoupper ( $string )
$string string|null

The input string.

return string

The uppercased string.

strtr() public static method

Translate certain characters.

public static string strtr ( $string, $replace_pairs = [] )
$string string

The string being translated.

$replace_pairs array

The replace_pairs parameter may be used as a substitute for to and from in which case it's an array in the form ['from' => 'to', ...].

return string

A copy of str, translating all occurrences of each character in from to the corresponding character in to.

substr() public static method

Return part of a string.

public static string|false substr ( $string, $offset, $length null )
$string string|null

The input string.

$offset integer

The starting offset position.

  • If the start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.
  • If start is negative, the returned string will start at the start'th character from the end of string.
  • If string is less than or equal to start characters long, false will be returned.

Example(s) of using a negative start:

$rest = Lib::substr("abcdef", -1);    // returns "f"
$rest = Lib::substr("abcdef", -2);    // returns "ef"
$rest = Lib::substr("abcdef", -3, 1); // returns "d"
$length integer|null

The length of characters to return.

  • If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).
  • If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes a position beyond this truncation, an empty string will be returned.
  • If length is given and is 0, then false or null or an empty string will be returned.

Example(s) of using a negative length:

$rest = Lib::substr("abcdef", 0, -1);  // returns "abcde"
$rest = Lib::substr("abcdef", 2, -1);  // returns "cde"
$rest = Lib::substr("abcdef", 4, -4);  // returns false
$rest = Lib::substr("abcdef", -3, -1); // returns "de"
return string|false

The extracted part of string or false on failure.

trim() public static method

Strip whitespace (or other characters) from the beginning and end of a string.

public static string trim ( $string, $characters null )
$string string|null

The string that will be trimmed.

$characters string

Optionally, the stripped characters can also be specified using the charlist parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.

return string

The trimmed string.

ucfirst() public static method

Make a string's first character uppercase

public static string ucfirst ( $string )
$string string

The input string.

return string

The resulting string.

ucwords() public static method

Uppercase the first character of each word in a string

public static string ucwords ( $string, $separators " \t\r\n\f\v" )
$string string

The input string.

$separators string

The optional separators contains the word separator characters.

return string

The modified string.

urldecode() public static method

Decodes URL-encoded string

public static string urldecode ( $string )
$string string

The string to be decoded.

return string

The decoded string.

urlencode() public static method

URL-encodes string.

public static string urlencode ( $string )
$string string

The string to be encoded.

return string

A string in which all non-alphanumeric characters except '*',' ','-','_','.' have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.