HumHub Documentation (unofficial)

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
author

Kartik Visweswaran kartikv2@gmail.com

since
1.0

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
link
https://php.net/manual/en/function.explode.php
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
link
https://php.net/manual/en/function.html-entity-decode.php
Return values
string

the decoded 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
link
https://php.net/manual/en/function.nl2br.php
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 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|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 of subject , 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
link
https://php.net/manual/en/function.preg-filter.php
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 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 : int = 0

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.

Tags
link
https://php.net/manual/en/function.preg-match.php
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 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 : int = 0

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.

Tags
link
https://php.net/manual/en/function.preg-match-all.php
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 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|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 of subject , 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
link
https://php.net/manual/en/function.preg-replace.php
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 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|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 and PREG_UNMATCHED_AS_NULL flags, which influence the format of the matches array. See the description in [[preg_match]] for more details.

Tags
link
https://php.net/manual/en/function.preg-replace-callback.php
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 and PREG_UNMATCHED_AS_NULL flags, which influence the format of the matches array. See the description in [[preg_match]] for more details.

Tags
link
https://php.net/manual/en/function.preg-replace-callback-array.php
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. 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 : 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 by preg_split.

Tags
link
https://php.net/manual/en/function.preg-split.php
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.

rawurlencode()

URL-encode according to RFC 3986

public static rawurlencode(string $string) : string
Parameters
$string : string

The string to be encoded.

Tags
link
https://php.net/manual/en/function.rawurlencode.php
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 multiple needles.

$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. 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 : int = null

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

Tags
link

Example:

$replaced = Lib::str_replace('ell', '-', 'Hello');
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 to 0, the function will return an empty string.

Tags
link
https://php.net/manual/en/function.str-repeat.php
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 multiple needles.

$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. 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 : int = null

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

Tags
link

Example:

$replaced = Lib::str_replace('ell', '-', 'Hello');
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
link
https://php.net/manual/en/function.strip-tags.php
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
link
https://php.net/manual/en/function.strrpos.php
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.

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
link
https://php.net/manual/en/function.strncmp.php
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
link
https://php.net/manual/en/function.strpos.php
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
link
https://php.net/manual/en/function.strrpos.php
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
link

Example:

$lower = Lib::strtolower($string);
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
link

Example:

$upper = Lib::strtoupper($string);
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
link

Example:

$replaced = Lib::strtr('Hello World!', ['o' => 'a', 'l' => 'r']);
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 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 : 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, 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"
Tags
link
https://php.net/manual/en/function.substr.php
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
link

Example:

$trimmed = Lib::trim($string);
Return values
string

The trimmed 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
link
https://php.net/manual/en/function.ucwords.php
Return values
string

the modified string.

urlencode()

URL-encodes string.

public static urlencode(string $string) : string
Parameters
$string : string

The string to be encoded.

Tags
link
https://php.net/manual/en/function.urlencode.php
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.

Loading…
On this page

Search results