Lib
in package
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 ');
Tags
Table of Contents
Methods
- explode() : array<string|int, string>|false
- Split a string by a string.
- html_entity_decode() : string
- Convert HTML entities to their corresponding characters.
- lcfirst() : string
- Make a string's first character lowercase
- nl2br() : string
- Inserts HTML line breaks before all newlines in a string
- preg_filter() : string|array<string|int, string>|null
- Perform a regular expression search and replace.
- preg_match() : int|false
- Perform a regular expression match
- preg_match_all() : int|false|null
- Perform a global regular expression match.
- preg_replace() : string|array<string|int, string>|null
- Perform a regular expression search and replace.
- preg_replace_callback() : string|array<string|int, string>|null
- Perform a regular expression search and replace using a callback.
- preg_replace_callback_array() : string|array<string|int, string>|null
- Perform a regular expression search and replace using callbacks.
- preg_split() : array<string|int, string>|false
- Split string by a regular expression.
- rawurldecode() : string
- Decode URL-encoded strings
- rawurlencode() : string
- URL-encode according to RFC 3986
- str_ireplace() : string|array<string|int, string>
- Case-insensitive version of [[str_replace]].
- str_repeat() : string
- Repeat a string.
- str_replace() : string|array<string|int, string>
- Replace all occurrences of the search string with the replacement string.
- strip_tags() : string
- Strip HTML and PHP tags from a string
- stripos() : int|false
- Find position of last occurrence of a case-insensitive string in a string
- strlen() : int
- Get string length.
- strncmp() : int
- Binary safe string comparison of the first n characters
- strpos() : int|false
- Find the position of the first occurrence of a substring in a string.
- strrpos() : int|false
- Find the position of the last occurrence of a substring in a string
- strtolower() : string
- Make a string lowercase.
- strtoupper() : string
- Make a string uppercase.
- strtr() : string
- Translate certain characters.
- substr() : string|false
- Return part of a string.
- trim() : string
- Strip whitespace (or other characters) from the beginning and end of a string.
- ucfirst() : string
- Make a string's first character uppercase
- ucwords() : string
- Uppercase the first character of each word in a string
- urldecode() : string
- Decodes URL-encoded string
- urlencode() : string
- URL-encodes string.
Methods
explode()
Split a string by a string.
public
static explode(string $separator, string $string[, int|null $limit = null ]) : array<string|int, string>|false
Parameters
- $separator : string
-
The boundary string.
- $string : string
-
The input string.
- $limit : int|null = 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
.
Tags
Return values
array<string|int, 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()
Convert HTML entities to their corresponding characters.
public
static html_entity_decode(string $string[, int $flags = ENT_COMPAT ][, string|null $encoding = null ]) : string
Parameters
- $string : string
-
The input string.
- $flags : int = ENT_COMPAT
-
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 = null
-
The ISO-8859-1 character set is used as default for the optional third charset. This defines the character set used in conversion.
Tags
Return values
string —the decoded string.
lcfirst()
Make a string's first character lowercase
public
static lcfirst(string $string) : string
Parameters
- $string : string
-
The input string.
Tags
Return values
string —the resulting string.
nl2br()
Inserts HTML line breaks before all newlines in a string
public
static nl2br(string $string[, bool $use_xhtml = true ]) : string
Parameters
- $string : string
-
The input string.
- $use_xhtml : bool = true
-
Whenever to use XHTML compatible line breaks or not.
Tags
Return values
string —the altered string.
preg_filter()
Perform a regular expression search and replace.
public
static preg_filter(string|array<string|int, string> $pattern, string|array<string|int, string> $replacement, string|array<string|int, string> $subject[, int $limit = -1 ][, int &$count = null ]) : string|array<string|int, string>|null
Parameters
- $pattern : string|array<string|int, 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|array<string|int, 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
andreplacement
parameters are arrays, eachpattern
will be replaced by thereplacement
counterpart. - If there are fewer elements in the
replacement
array than in thepattern
array, any extrapattern
s 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 then
'th parenthesized pattern.n
can be from0
to99
, and\\0
or$0
refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from1
) 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 confusepreg_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 the1
as a literal. - When using the deprecated
e
modifier, this function escapes some characters (namely'
,"
,\
andNULL
) 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.
- If this parameter is a string and the
- $subject : string|array<string|int, 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 ofsubject
, and the return value is an array as well. - $limit : int = -1
-
The maximum possible replacements for each pattern in each
subject
string. Defaults to-1
(no limit). - $count : int = null
-
If specified, this variable will be filled with the number of replacements done.
Tags
Return values
string|array<string|int, 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()
Perform a regular expression match
public
static preg_match(string $pattern, string $subject, array<string|int, string> &$matches[, int $flags = 0 ][, int $offset = 0 ]) : int|false
Parameters
- $pattern : string
-
The pattern to search for, as a string.
- $subject : string
-
The input string.
- $matches : array<string|int, 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 : int = 0
-
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 ofmatches
into an array where every element is an array consisting of the matched string at offset0
and its string offset intosubject
at offset1
.
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 : int = 0
-
Normally, the search starts from the beginning of the
subject
string. The optional parameteroffset
can be used to specify the alternate place from which to start the search (in bytes). Usingoffset
is not equivalent to passing substr($subject, $offset) topreg_match
in place of thesubject
string, becausepattern
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 theA
modifier instead, both of which work with the offset parameter.
Tags
Return values
int|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()
Perform a global regular expression match.
public
static preg_match_all(string $pattern, string $subject, array<string|int, array<string|int, string>> &$matches[, int $flags = PREG_PATTERN_ORDER ][, int $offset = 0 ]) : int|false|null
Parameters
- $pattern : string
-
The pattern to search for, as a string.
- $subject : string
-
The input string.
- $matches : array<string|int, array<string|int, string>>
-
Array of all matches in multi-dimensional array ordered according to flags.
- $flags : int = PREG_PATTERN_ORDER
-
Can be a combination of the following flags (note that it doesn't make sense to use
PREG_PATTERN_ORDER
together withPREG_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 offset0
and its string offset intosubject
at offset1
.
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 asNULL
; otherwise they are reported as an empty string.
If no order flag is given,
PREG_PATTERN_ORDER
is assumed. -
- $offset : int = 0
-
Normally, the search starts from the beginning of the
subject
string. The optional parameteroffset
can be used to specify the alternate place from which to start the search (in bytes). Usingoffset
is not equivalent to passing substr($subject, $offset) topreg_match_all
in place of thesubject
string, becausepattern
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.
Tags
Return values
int|false|null —the number of full pattern matches (which might be zero), or FALSE
if an error
occurred.
preg_replace()
Perform a regular expression search and replace.
public
static preg_replace(string|array<string|int, string> $pattern, string|array<string|int, string> $replacement, string|array<string|int, string> $subject[, int $limit = -1 ][, int &$count = null ]) : string|array<string|int, string>|null
Parameters
- $pattern : string|array<string|int, 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|array<string|int, 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
andreplacement
parameters are arrays, eachpattern
will be replaced by thereplacement
counterpart. - If there are fewer elements in the
replacement
array than in thepattern
array, any extrapattern
s 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 then
'th parenthesized pattern.n
can be from0
to99
, and\\0
or$0
refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from1
) 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 confusepreg_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 the1
as a literal. - When using the deprecated
e
modifier, this function escapes some characters (namely'
,"
,\
andNULL
) 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.
- If this parameter is a string and the
- $subject : string|array<string|int, 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 ofsubject
, and the return value is an array as well. - $limit : int = -1
-
The maximum possible replacements for each pattern in each
subject
string. Defaults to-1
(no limit). - $count : int = null
-
If specified, this variable will be filled with the number of replacements done.
Tags
Return values
string|array<string|int, 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()
Perform a regular expression search and replace using a callback.
public
static preg_replace_callback(string|array<string|int, string> $pattern, callable $callback, string|array<string|int, string> $subject[, int $limit = -1 ][, int &$count = null ][, int $flags = 0 ]) : string|array<string|int, string>|null
Parameters
- $pattern : string|array<string|int, 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 apreg_replace_callback
in just one place. In this case you can use an anonymous function to declare the callback within the call topreg_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|array<string|int, string>
-
The string or an array with strings to search and replace.
- $limit : int = -1
-
The maximum possible replacements for each pattern in each
subject
string. Defaults to-1
(no limit). - $count : int = null
-
If specified, this variable will be filled with the number of replacements done.
- $flags : int = 0
-
can be a combination of the
PREG_OFFSET_CAPTURE
andPREG_UNMATCHED_AS_NULL
flags, which influence the format of thematches
array. See the description in [[preg_match]] for more details.
Tags
Return values
string|array<string|int, 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()
Perform a regular expression search and replace using callbacks.
public
static preg_replace_callback_array(array<string|int, mixed>|array<string|int, callable> $pattern, string|array<string|int, string> $subject[, int $limit = -1 ][, int &$count = null ][, int $flags = 0 ]) : string|array<string|int, string>|null
Parameters
- $pattern : array<string|int, mixed>|array<string|int, callable>
-
An associative array mapping patterns (keys) to callbacks (values)
- $subject : string|array<string|int, string>
-
The string or an array with strings to search and replace.
- $limit : int = -1
-
The maximum possible replacements for each pattern in each
subject
string. Defaults to-1
(no limit). - $count : int = null
-
If specified, this variable will be filled with the number of replacements done.
- $flags : int = 0
-
can be a combination of the
PREG_OFFSET_CAPTURE
andPREG_UNMATCHED_AS_NULL
flags, which influence the format of thematches
array. See the description in [[preg_match]] for more details.
Tags
Return values
string|array<string|int, 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()
Split string by a regular expression.
public
static preg_split(string $pattern, string $subject[, int $limit = -1 ][, int $flags = 0 ]) : array<string|int, string>|false
Parameters
- $pattern : string
-
The pattern to search for, as a string.
- $subject : string
-
The input string.
- $limit : int = -1
-
If specified, then only substrings up to
limit
are returned with the rest of the string being placed in the last substring. Alimit
of-1
,0
orNULL
means "no limit" and, as is standard across PHP, you can useNULL
to skip to theflags
parameter. - $flags : int = 0
-
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 bypreg_split
.
Tags
Return values
array<string|int, string>|false —an array containing substrings of subject
split along boundaries matched by pattern
,
or FALSE
if an error occurred.
rawurldecode()
Decode URL-encoded strings
public
static rawurldecode(string $string) : string
Parameters
- $string : string
-
The URL to be encoded.
Tags
Return values
string —the decoded URL, as a string.
rawurlencode()
URL-encode according to RFC 3986
public
static rawurlencode(string $string) : string
Parameters
- $string : string
-
The string to be encoded.
Tags
Return values
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()
Case-insensitive version of [[str_replace]].
public
static str_ireplace(string|array<string|int, string> $search, string|array<string|int, string> $replace, string|array<string|int, string> $subject[, int &$count = null ]) : string|array<string|int, string>
Parameters
- $search : string|array<string|int, string>
-
The value being searched for, otherwise known as the
needle
. An array may be used to designate multipleneedles
. - $replace : string|array<string|int, string>
-
The replacement value that replaces found search values. An array may be used to designate multiple
replacements
. - $subject : string|array<string|int, string>
-
The string or array being searched and replaced on, otherwise known as the
haystack
. Ifsubject
is an array, then the search and replace is performed with every entry ofsubject
, and the return value is an array as well. - $count : int = null
-
If passed, this will hold the number of matched and replaced
needles
.
Tags
Return values
string|array<string|int, string> —This function returns a string or an array with the replaced values.
str_repeat()
Repeat a string.
public
static str_repeat(string $string[, int|null $times = null ]) : string
Parameters
- $string : string
-
The string to be repeated.
- $times : int|null = 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 to0
, the function will return an empty string.
Tags
Return values
string —the repeated string.
str_replace()
Replace all occurrences of the search string with the replacement string.
public
static str_replace(string|array<string|int, string> $search, string|array<string|int, string> $replace, string|array<string|int, string> $subject[, int &$count = null ]) : string|array<string|int, string>
Parameters
- $search : string|array<string|int, string>
-
The value being searched for, otherwise known as the
needle
. An array may be used to designate multipleneedles
. - $replace : string|array<string|int, string>
-
The replacement value that replaces found search values. An array may be used to designate multiple replacements.
- $subject : string|array<string|int, string>
-
The string or array being searched and replaced on, otherwise known as the
haystack
. Ifsubject
is an array, then the search and replace is performed with every entry ofsubject
, and the return value is an array as well. - $count : int = null
-
If passed, this will hold the number of matched and replaced
needles
.
Tags
Return values
string|array<string|int, string> —This function returns a string or an array with the replaced values.
strip_tags()
Strip HTML and PHP tags from a string
public
static strip_tags(string $string[, array<string|int, string>|string|null $allowed_tags = null ]) : string
Parameters
- $string : string
-
The input string.
- $allowed_tags : array<string|int, string>|string|null = 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.
Tags
Return values
string —the stripped string.
stripos()
Find position of last occurrence of a case-insensitive string in a string
public
static stripos(string $haystack, string $needle[, int $offset = 0 ]) : int|false
Parameters
- $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 : int = 0
-
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.
Tags
Return values
int|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()
Get string length.
public
static strlen(string $string) : int
Parameters
- $string : string
-
The string being measured for length.
Tags
Return values
int —The length of the string on success, and 0
if the string is empty.
strncmp()
Binary safe string comparison of the first n characters
public
static strncmp(string $string1, string $string2, int $length) : int
Parameters
- $string1 : string
-
The first string.
- $string2 : string
-
The second string.
- $length : int
-
Number of characters to use in the comparison.
Tags
Return values
int —< 0
if str1
is less than str2
, > 0
if str1
is greater than str2
, and = 0
if they are
equal.
strpos()
Find the position of the first occurrence of a substring in a string.
public
static strpos(string $haystack, string $needle[, int $offset = 0 ]) : int|false
Parameters
- $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 : int = 0
-
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.
Tags
Return values
int|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()
Find the position of the last occurrence of a substring in a string
public
static strrpos(string $haystack, string $needle[, int $offset = 0 ]) : int|false
Parameters
- $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 : int = 0
-
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.
Tags
Return values
int|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()
Make a string lowercase.
public
static strtolower(string|null $string) : string
Parameters
- $string : string|null
-
The input string.
Tags
Return values
string —the lowercased string.
strtoupper()
Make a string uppercase.
public
static strtoupper(string|null $string) : string
Parameters
- $string : string|null
-
The input string.
Tags
Return values
string —the uppercased string.
strtr()
Translate certain characters.
public
static strtr(string $string[, array<string|int, mixed> $replace_pairs = [] ]) : string
Parameters
- $string : string
-
The string being translated.
- $replace_pairs : array<string|int, mixed> = []
-
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', ...]
.
Tags
Return values
string —A copy of str, translating all occurrences of each character in from
to the corresponding
character in to
.
substr()
Return part of a string.
public
static substr(string|null $string, int $offset[, int|null $length = null ]) : string|false
Parameters
- $string : string|null
-
The input string.
- $offset : int
-
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 position2
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"
- 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
- $length : int|null = 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
, thenfalse
ornull
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"
Tags
Return values
string|false —the extracted part of string or false on failure.
trim()
Strip whitespace (or other characters) from the beginning and end of a string.
public
static trim(string|null $string[, string $characters = null ]) : string
Parameters
- $string : string|null
-
The string that will be trimmed.
- $characters : string = null
-
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.
Tags
Return values
string —The trimmed string.
ucfirst()
Make a string's first character uppercase
public
static ucfirst(string $string) : string
Parameters
- $string : string
-
The input string.
Tags
Return values
string —the resulting string.
ucwords()
Uppercase the first character of each word in a string
public
static ucwords(string $string[, string $separators = "
" ]) : string
Parameters
- $string : string
-
The input string.
- $separators : string = " "
-
The optional separators contains the word separator characters.
Tags
Return values
string —the modified string.
urldecode()
Decodes URL-encoded string
public
static urldecode(string $string) : string
Parameters
- $string : string
-
The string to be decoded.
Tags
Return values
string —the decoded string.
urlencode()
URL-encodes string.
public
static urlencode(string $string) : string
Parameters
- $string : string
-
The string to be encoded.
Tags
Return values
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.