Class kartik\base\Lib
Inheritance | kartik\base\Lib |
---|---|
Available since version | 1.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
Method | Description | Defined 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
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 |
return | string[]|false | If delimiter is an empty string ( |
---|
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
|
$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. |
---|
Make a string's first character lowercase
public static string lcfirst ( $string ) | ||
$string | string | The input string. |
return | string | The resulting string. |
---|
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. |
---|
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 |
$replacement | string|string[] | The string or an array with strings to replace.
|
$subject | string|string[] | The string or an array with strings to search and replace. If |
$limit | integer | The maximum possible replacements for each pattern in each |
$count | integer | If specified, this variable will be filled with the number of replacements done. |
return | string|string[]|null | An array if the |
---|
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 |
$flags | integer | flags can be the following flags:
The above example will output:
The above example will output:
|
$offset | integer | Normally, the search starts from the beginning of the Compare:
The above example will output:
while this example
will produce:
Alternatively, to avoid using |
return | integer|false |
|
---|
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
The above example will output:
So, 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
The above example will output:
The above example will output
The above example will output
If no order flag is given, |
$offset | integer | Normally, the search starts from the beginning of the
The above example will output:
So, |
return | integer|false|null | The number of full pattern matches (which might be zero), or |
---|
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.
|
$subject | string|string[] | The string or an array with strings to search and replace. If |
$limit | integer | The maximum possible replacements for each pattern in each |
$count | integer | If specified, this variable will be filled with the number of replacements done. |
return | string|string[]|null |
|
---|
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
You'll often need the Example of usage of
|
$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 |
$count | integer | If specified, this variable will be filled with the number of replacements done. |
$flags | integer | Can be a combination of the |
return | string|string[]|null |
|
---|
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 |
$count | integer | If specified, this variable will be filled with the number of replacements done. |
$flags | integer | Can be a combination of the |
return | string|string[]|null |
|
---|
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 |
$flags | integer |
|
return | string[]|false | An array containing substrings of |
---|
Decode URL-encoded strings
public static string rawurldecode ( $string ) | ||
$string | string | The URL to be encoded. |
return | string | The decoded URL, as a string. |
---|
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
( |
---|
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 |
$replace | string|string[] | The replacement value that replaces found search values. An array may be used
to designate multiple |
$subject | string|string[] | The string or array being searched and replaced on, otherwise known as the
|
$count | integer | If passed, this will hold the number of matched and replaced |
return | string|string[] | This function returns a string or an array with the replaced values. |
---|
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 |
return | string | The repeated string. |
---|
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 |
$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
|
$count | integer | If passed, this will hold the number of matched and replaced |
return | string|string[] | This function returns a string or an array with the replaced values. |
---|
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. |
---|
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 |
$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 |
---|
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 |
---|
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 |
|
---|
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 |
$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 |
---|
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 |
$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 |
---|
Make a string lowercase.
public static string strtolower ( $string ) | ||
$string | string|null | The input string. |
return | string | The lowercased string. |
---|
Make a string uppercase.
public static string strtoupper ( $string ) | ||
$string | string|null | The input string. |
return | string | The uppercased string. |
---|
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 |
return | string | A copy of str, translating all occurrences of each character in |
---|
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.
Example(s) of using a negative start:
|
$length | integer|null | The length of characters to return.
Example(s) of using a negative length:
|
return | string|false | The extracted part of string or false on failure. |
---|
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 |
return | string | The trimmed string. |
---|
Make a string's first character uppercase
public static string ucfirst ( $string ) | ||
$string | string | The input string. |
return | string | The resulting string. |
---|
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. |
---|
Decodes URL-encoded string
public static string urldecode ( $string ) | ||
$string | string | The string to be decoded. |
return | string | The decoded string. |
---|
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 |
---|