Skip to main content

Module encoding

Module encoding 

Source
Expand description

Encodings: URL encoding, http_build_query, base64, hex, HTML entities and quoted-printable.

Ported from PHP 8.5.11 (ext/standard/url.c, http.c, base64.c, string.c, html.c, quot_print.c). Byte strings in, byte strings out; encoders whose output is always ASCII return String.

PHPRust
urlencode, rawurlencode, urldecode, rawurldecodeurlencode, rawurlencode, urldecode, rawurldecode
http_build_queryhttp_build_query with QueryEncoding
parse_strparse_str
base64_encode, base64_decodebase64_encode, base64_decode
bin2hex, hex2binbin2hex, hex2bin
htmlspecialchars, htmlentitieshtmlspecialchars, htmlentities with HtmlFlags
quoted_printable_encodequoted_printable_encode

The HTML functions support PHP’s default charset, UTF-8. Their entity tables (encoding/html_tables.rs) are generated by data/html_entities.py from php-src’s ext/standard/html_tables.h and PHP’s own get_html_translation_table() (data/html_entities.php).

Structs§

HtmlFlags
The $flags of the HTML functions, decoded from PHP’s bit mask.

Enums§

Doctype
The document type of the ENT_HTML401/ENT_XML1/ENT_XHTML/ENT_HTML5 flags.
QueryEncoding
http_build_query()’s $encoding_type.

Constants§

MAX_INPUT_NESTING_LEVEL
max_input_nesting_level: bracket levels in a variable name, PHP’s default.
MAX_INPUT_VARS
max_input_vars: variables registered per input, PHP’s default.

Functions§

base64_decode
PHP base64_decode($string, $strict). Non-strict skips every byte that is not in the alphabet (and decodes data after = padding); strict skips only \t \n \r and spaces and fails (None, PHP’s false) on anything else, on data after padding, on a lone final character or on bad padding.
base64_encode
PHP base64_encode: standard alphabet with = padding.
bin2hex
PHP bin2hex: lowercase hexadecimal.
hex2bin
PHP hex2bin: None (PHP’s false, with a warning) for an odd length or a non-hexadecimal character.
htmlentities
PHP htmlentities($string, $flags, 'UTF-8', $double_encode): like htmlspecialchars but every character with a named entity in the doctype’s table is converted (HTML 4.01 and XHTML: 253 entities, HTML5: 1511 including two-character ones such as fj; XML 1.0: the basic ones).
htmlspecialchars
PHP htmlspecialchars($string, $flags, 'UTF-8', $double_encode): escapes & < > and the quotes flags selects (' as &#039; in HTML 4.01, &apos; otherwise). With double_encode false, existing valid entities are kept. Invalid UTF-8 follows ENT_IGNORE/ENT_SUBSTITUTE, else the result is empty.
http_build_query
PHP http_build_query($data, $numeric_prefix, $arg_separator, $encoding_type) for data decoded from JSON (objects are PHP arrays; keys that are canonical integers are integer keys, which get numeric_prefix at the top level). Nested arrays become a%5Bb%5D=...; null values are skipped, booleans are 1/0, floats use the precision setting. arg_separator None is PHP’s arg_separator.output default, &.
is_integer_key
Whether a PHP array key string is an integer key (ZEND_HANDLE_NUMERIC_STR): -?[1-9][0-9]* or 0, within i64.
parse_str
parse_str($string, $result): php_default_treat_data(PARSE_STRING) with the default arg_separator.input (&), max_input_vars and max_input_nesting_level. Also how Swoole fills $request->get and, for application/x-www-form-urlencoded bodies, $request->post.
quoted_printable_encode
PHP quoted_printable_encode (RFC 2045, lines of at most 76 bytes with PHP’s look-ahead for multi-byte UTF-8 lead bytes).
rawurldecode
PHP rawurldecode: %XX is a byte (+ stays +).
rawurldecode_into
rawurldecode, appended to out instead of a new buffer.
rawurlencode
PHP rawurlencode (RFC 3986): alphanumerics and -_.~ are kept, every other byte is %XX.
register_variable
php_register_variable_ex($name, $value, $array): registers one request variable the way PHP fills $_GET, $_POST and $_COOKIE. Leading spaces are dropped, . and in the base name become _, name[a][] builds nested arrays (at most MAX_INPUT_NESTING_LEVEL levels; deeper names drop the whole variable), and an unterminated [ is kept as _.
urldecode
PHP urldecode: + is a space and %XX a byte; a % not followed by two hex digits is kept.
urldecode_into
urldecode, appended to out instead of a new buffer.
urlencode
PHP urlencode: application/x-www-form-urlencoded (RFC 1738): alphanumerics and -_. are kept, a space is +, every other byte (including ~) is %XX.