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.
| PHP | Rust |
|---|---|
strtolower, strtoupper, ucfirst, lcfirst, ucwords | strtolower, strtoupper, ucfirst, lcfirst, ucwords |
trim, ltrim, rtrim | trim, ltrim, rtrim (TRIM_CHARACTERS is the default) |
substr | substr |
stripos | stripos |
str_contains, str_starts_with | str_contains, str_starts_with |
strcmp, strcasecmp | strcmp, strcasecmp |
str_replace | str_replace, str_replace_array |
strtr | strtr, strtr_array |
explode | explode |
addcslashes, stripcslashes | addcslashes, stripcslashes |
strip_tags (no allowed tags) | strip_tags |
escapeshellarg | escapeshellarg |
ctype_alnum, ctype_digit, ctype_xdigit | ctype_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
$replaceof an array-$searchstr_replace.
Constants§
- NO_
LIMIT explode’s default$limit(PHP_INT_MAX): no limit.- TRIM_
CHARACTERS - The default character list of
trim,ltrimandrtrim:" \n\r\t\v\0". - UCWORDS_
SEPARATORS - The default
$separatorsofucwords:" \t\r\n\f\v".
Functions§
- addcslashes
- PHP
addcslashes($string, $characters): backslash-escapes the bytes ofcharacters(witha..zranges); those outside printable ASCII become\n,\t,\r,\a,\v,\b,\for a 3-digit octal escape. - charmask
php_charmask: the set of bytes atrim/ucwords/addcslashescharacter list names.a..zadds an inclusive range; a malformed range (PHP warns) adds what PHP adds: for"a.."that isaand the last..- crypt
- PHP
crypt($string, $salt): the hash, or*0(*1when the salt starts with*0) where the salt names no supported algorithm or is invalid. - ctype_
alnum - PHP
ctype_alnumon a string: non-empty and all ASCII letters or digits. - ctype_
digit - PHP
ctype_digiton a string: non-empty and all0-9. - ctype_
xdigit - PHP
ctype_xdigiton 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 ofseparator. A positivelimitreturns at most that many pieces (the last holds the rest); a negative one drops that many pieces from the end;0is1. An empty string gives[""](or[]for a negative limit). - hash
hash($algo, $data)as lowercase hex, for every algorithm ofhash_algos();Nonefor 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): liketrim, 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_costandthreads(already cast to int) andsalt(16 random bytes in PHP).- password_
hash_ bcrypt password_hash($password, PASSWORD_BCRYPT, ['cost' => $cost])withsalt(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)(aliaschop): liketrim, end only. - str_
contains - PHP
str_contains. - str_
replace - PHP
str_replace($search, $replace, $subject, $count)with string arguments: replaces every occurrence ofsearch(left to right, not overlapping) and returns the result with$count. An emptysearchchanges nothing. Valid UTF-8 arguments give valid UTF-8. - str_
replace_ array - PHP
str_replace/str_ireplacewith 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$subjectapply this to each element (keys kept, counts summed); a string$searchwith an array$replaceis aTypeErrorin PHP and cannot be expressed here. - str_
starts_ with - PHP
str_starts_with. - strcasecmp
- PHP
strcasecmp:strcmpon ASCII-lowercased bytes. - strcmp
- PHP
strcmp: the byte difference at the first mismatch ("a"vs"c"is-2), else-1,0or1by 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 afteroffset(negative: from the end),Noneforfalse. An empty needle matches at the offset. - strtolower
- PHP
strtolower: ASCIIA-Ztoa-z; every other byte unchanged. Borrows when nothing changes. Valid UTF-8 stays valid UTF-8. - strtoupper
- PHP
strtoupper: ASCIIa-ztoA-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 offromto the byte at the same position ofto, ignoring the extra bytes of the longer one. When a byte appears twice infrom, 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).pairsis 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 negativeoffsetcounts from the end; a negativelengthstops that many bytes before the end;Nonetakes 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):nullandfalseare"",trueis"1", integers in decimal, floats at theprecisionsetting (number::to_string), arrays"Array"(PHP also warns). AstdClass(an empty JSON object) has no string form: PHP’sError. - trim
- PHP
trim($string, $characters): strips the bytes ofcharacters(witha..zranges, seecharmask) from both ends. PassTRIM_CHARACTERSfor 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 ofseparators.separatorsacceptsa..zranges liketrim. PassUCWORDS_SEPARATORSfor PHP’s default.