Encode
Reference for the Encode module: safely encode a value for a given output context - HTML, a URL, JSON, a SQL identifier or number, or a path segment. Every function with its full signature, parameters and return type.
This page is a pure reference. For how the framework fits together, see the Guide.
Encode- static functions that encode a value for a given output context.EncodeError- thrown when a value cannot be encoded to JSON.
Encode
Static functions that take a value and return it encoded for a particular output context, listed alphabetically. Each converts its input with the To helpers before encoding, and several run a Security check on the input first.
Html
Encode::Html($text): string
Encode a value as text for HTML output, escaping special characters with htmlspecialchars.
| Name | Type | Description |
|---|---|---|
$text | mixed | The value to encode; converted to a string first. |
Returns string The HTML-escaped text.
HtmlNum
Encode::HtmlNum($value): string
Encode a value as a number for HTML output.
| Name | Type | Description |
|---|---|---|
$value | mixed | The value to encode; converted to a number first. |
Returns string The HTML-escaped number.
HtmlUrl
Encode::HtmlUrl($text): string
URL-encode a value and then HTML-escape it, for a URL placed in HTML.
| Name | Type | Description |
|---|---|---|
$text | mixed | The value to encode; asserted to be a string, then converted to a string. |
Returns string The URL-encoded, HTML-escaped text.
HtmlUrlArray
Encode::HtmlUrlArray($array): string
Build a URL query string from an array and HTML-escape it.
| Name | Type | Description |
|---|---|---|
$array | array | The name-to-value pairs; security-checked as an array first. |
Returns string The HTML-escaped query string.
Json
Encode::Json(mixed $data, bool $pretty = false): string
Encode a value as JSON, dropping null values, and optionally pretty-printing.
| Name | Type | Description |
|---|---|---|
$data | mixed | The value to encode; objects are cast to arrays and null values are removed recursively. |
$pretty | bool | When true, format the output with indentation. Defaults to false. |
Returns string The JSON text. Throws EncodeError when the value cannot be encoded.
JsonBoolean
Encode::JsonBoolean($data): string
Encode a value as a JSON boolean.
| Name | Type | Description |
|---|---|---|
$data | mixed | The value to encode; converted to true or false first. |
Returns string The JSON boolean.
JsonNum
Encode::JsonNum($data, $min = null, $max = null): string
Encode a value as a JSON number, clamped to an optional range.
| Name | Type | Description |
|---|---|---|
$data | mixed | The value to encode; converted to a number first. |
$min | mixed | The minimum allowed value, or null for no minimum. Defaults to null. |
$max | mixed | The maximum allowed value, or null for no maximum. Defaults to null. |
Returns string The JSON number.
JsonStr
Encode::JsonStr($data): string
Encode a value as a JSON string.
| Name | Type | Description |
|---|---|---|
$data | mixed | The value to encode; converted to a string first. |
Returns string The JSON string.
PathDate
Encode::PathDate($date): string
Encode a value as a date for use in a path.
| Name | Type | Description |
|---|---|---|
$date | mixed | The value to encode; security-checked as a date, then converted to a date. |
Returns string The date as a path segment.
PathID
Encode::PathID($id): int
Encode a value as an id for use in a path.
| Name | Type | Description |
|---|---|---|
$id | mixed | The value to encode; security-checked as an id, then converted to an id. |
Returns int The id.
SqlDatabaseName
Encode::SqlDatabaseName($text): string
Encode a value as a SQL database name.
| Name | Type | Description |
|---|---|---|
$text | mixed | The value to encode; security-checked as a database name, then converted to a database name. |
Returns string The database name.
SqlID
Encode::SqlID($id): int
Encode a value as a SQL id.
| Name | Type | Description |
|---|---|---|
$id | mixed | The value to encode; converted to an id. |
Returns int The id.
SqlNum
Encode::SqlNum($id): int
Encode a value as a SQL number.
| Name | Type | Description |
|---|---|---|
$id | mixed | The value to encode; converted to a number. |
Returns int The number.
SqlNumArray
Encode::SqlNumArray(array $array): array
Encode every element of an array as a SQL number.
| Name | Type | Description |
|---|---|---|
$array | array | The values to encode; each element is converted to a number. |
Returns array The list of encoded numbers.
Url
Encode::Url($text): string
URL-encode a value with urlencode.
| Name | Type | Description |
|---|---|---|
$text | mixed | The value to encode; converted to a string first. |
Returns string The URL-encoded text.
UrlArray
Encode::UrlArray($array): string
Build a URL query string from an array, URL-encoding each name and value and joining them with ampersands.
| Name | Type | Description |
|---|---|---|
$array | array | The name-to-value pairs; security-checked as an array first. |
Returns string The query string.
UrlID
Encode::UrlID($value): int
URL-encode a value as an id.
| Name | Type | Description |
|---|---|---|
$value | mixed | The value to encode; converted to an id, then URL-encoded. |
Returns int The URL-encoded id.
UrlNum
Encode::UrlNum($value): string
URL-encode a value as a number.
| Name | Type | Description |
|---|---|---|
$value | mixed | The value to encode; converted to a number, then URL-encoded. |
Returns string The URL-encoded number.
EncodeError
An exception thrown by Encode::Json when a value cannot be encoded to JSON.
Extends the built-in Exception and adds no members of its own.