Skip to main content

Module string

Module string 

Source
Expand description

Byte-string functions of ext/standard/string.c and ext/ctype, plus crypt(), password_hash() / password_verify() and hash().

PHP strings are byte strings: every function here takes and returns &[u8] / Vec<u8>, never assumes UTF-8 and handles invalid UTF-8 like any other byte. Case mapping (strtolower, ucfirst, stripos, …) is ASCII-only, as in PHP 8.2 and later.

Callers holding a &str pass s.as_bytes(). For valid UTF-8 arguments these keep the result valid UTF-8, so String::from_utf8 (or std::str::from_utf8) on it cannot fail: the case functions, trim family with an ASCII character list (such as TRIM_CHARACTERS), str_replace/strtr_array with UTF-8 strings, explode. Functions that work on single bytes or byte positions (substr, strtr with non-ASCII bytes) may split a character, as in PHP.

Arguments PHP takes as int (offsets, lengths) are i64, with PHP’s negative-offset rules. Results that PHP returns as false are None. Errors PHP throws are Error, one variant per exception class, with PHP’s message.

PHPRust
strtolower, strtoupper, ucfirst, lcfirst, ucwordsstrtolower, strtoupper, ucfirst, lcfirst, ucwords
trim, ltrim, rtrimtrim, ltrim, rtrim (TRIM_CHARACTERS is the default)
substrsubstr
striposstripos
str_contains, str_starts_withstr_contains, str_starts_with
strcmp, strcasecmpstrcmp, strcasecmp
str_replacestr_replace, str_replace_array
strtrstrtr, strtr_array
explodeexplode
addcslashes, stripcslashesaddcslashes, stripcslashes
strip_tags (no allowed tags)strip_tags
escapeshellargescapeshellarg
ctype_alnum, ctype_digit, ctype_xdigitctype_alnum, ctype_digit, ctype_xdigit (Ctype for any value)
(string) $value (zval_get_string)to_php_string

Enums§

Ctype
A ctype_* function, for calls with a value that may not be a string.
Error
An exception a PHP string, multibyte, encoding or formatting function throws: one variant per PHP exception class, carrying PHP’s message.
Replace
The $replace of an array-$search str_replace.

Constants§

NO_LIMIT
explode’s default $limit (PHP_INT_MAX): no limit.
TRIM_CHARACTERS
The default character list of trim, ltrim and rtrim: " \n\r\t\v\0".
UCWORDS_SEPARATORS
The default $separators of ucwords: " \t\r\n\f\v".

Functions§

addcslashes
PHP addcslashes($string, $characters): backslash-escapes the bytes of characters (with a..z ranges); those outside printable ASCII become \n, \t, \r, \a, \v, \b, \f or a 3-digit octal escape.
charmask
php_charmask: the set of bytes a trim/ucwords/addcslashes character list names. a..z adds an inclusive range; a malformed range (PHP warns) adds what PHP adds: for "a.." that is a and the last ..
crypt
PHP crypt($string, $salt): the hash, or *0 (*1 when the salt starts with *0) where the salt names no supported algorithm or is invalid.
ctype_alnum
PHP ctype_alnum on a string: non-empty and all ASCII letters or digits.
ctype_digit
PHP ctype_digit on a string: non-empty and all 0-9.
ctype_xdigit
PHP ctype_xdigit on a string: non-empty and all hexadecimal digits.
escapeshellarg
PHP escapeshellarg($arg) on Unix: the argument between single quotes, each ' written as '\'', so a POSIX shell reads it back as one word.
explode
PHP explode($separator, $string, $limit): the pieces between occurrences of separator. A positive limit returns at most that many pieces (the last holds the rest); a negative one drops that many pieces from the end; 0 is 1. An empty string gives [""] (or [] for a negative limit).
hash
hash($algo, $data) as lowercase hex, for every algorithm of hash_algos(); None for any other name (PHP’s “must be a valid hashing algorithm”). The name is case-insensitive.
lcfirst
PHP lcfirst: lowercases the first byte if it is an ASCII letter.
ltrim
PHP ltrim($string, $characters): like trim, start only.
md5_hex
md5($data): the lowercase hex digest, in a buffer rather than a new string.
password_hash_argon2id
password_hash($password, PASSWORD_ARGON2ID, $options) with the options’ memory_cost, time_cost and threads (already cast to int) and salt (16 random bytes in PHP).
password_hash_bcrypt
password_hash($password, PASSWORD_BCRYPT, ['cost' => $cost]) with salt (16 random bytes in PHP).
password_verify
PHP password_verify($password, $hash).
php_crypt
php_crypt(): crypt() without the failure strings. Both arguments are C strings (they end at the first NUL).
rtrim
PHP rtrim($string, $characters) (alias chop): like trim, end only.
str_contains
PHP str_contains.
str_replace
PHP str_replace($search, $replace, $subject, $count) with string arguments: replaces every occurrence of search (left to right, not overlapping) and returns the result with $count. An empty search changes nothing. Valid UTF-8 arguments give valid UTF-8.
str_replace_array
PHP str_replace / str_ireplace with an array $search: applies each search/replacement pair in turn to the result of the previous one, skipping empty search strings, and returns the total count. The array forms of $subject apply this to each element (keys kept, counts summed); a string $search with an array $replace is a TypeError in PHP and cannot be expressed here.
str_starts_with
PHP str_starts_with.
strcasecmp
PHP strcasecmp: strcmp on ASCII-lowercased bytes.
strcmp
PHP strcmp: the byte difference at the first mismatch ("a" vs "c" is -2), else -1, 0 or 1 by length.
strip_tags
PHP strip_tags($s) (no allowed tags): drops HTML and PHP tags, comments and NUL bytes; a < followed by whitespace is kept as text.
stripcslashes
PHP stripcslashes: decodes C-style escapes (\n, \t, \xhh, \ooo, …); an unknown escape yields the escaped byte.
stripos
PHP stripos($haystack, $needle, $offset): byte position of the first ASCII case-insensitive occurrence at or after offset (negative: from the end), None for false. An empty needle matches at the offset.
strtolower
PHP strtolower: ASCII A-Z to a-z; every other byte unchanged. Borrows when nothing changes. Valid UTF-8 stays valid UTF-8.
strtoupper
PHP strtoupper: ASCII a-z to A-Z; every other byte unchanged. Borrows when nothing changes. Valid UTF-8 stays valid UTF-8.
strtr
PHP strtr($string, $from, $to): translates each byte of from to the byte at the same position of to, ignoring the extra bytes of the longer one. When a byte appears twice in from, the last one wins.
strtr_array
PHP strtr($string, $replace_pairs): at each position replaces the longest key that matches, then continues after it; replaced text is never rescanned. Empty keys are ignored (PHP warns). pairs is the array in order, keys as PHP strings (integer keys as their decimal form); when a key repeats, the last pair wins, as in a PHP array.
substr
PHP substr($string, $offset, $length): a negative offset counts from the end; a negative length stops that many bytes before the end; None takes the rest. Out-of-range values clamp; never fails.
to_php_string
PHP’s string conversion of a value as string functions perform it (zval_get_string): null and false are "", true is "1", integers in decimal, floats at the precision setting (number::to_string), arrays "Array" (PHP also warns). A stdClass (an empty JSON object) has no string form: PHP’s Error.
trim
PHP trim($string, $characters): strips the bytes of characters (with a..z ranges, see charmask) from both ends. Pass TRIM_CHARACTERS for PHP’s default.
ucfirst
PHP ucfirst: uppercases the first byte if it is an ASCII letter.
ucwords
PHP ucwords($string, $separators): uppercases (ASCII) the first byte and every byte that follows one of separators. separators accepts a..z ranges like trim. Pass UCWORDS_SEPARATORS for PHP’s default.