Context specific methods for use in secure output escaping

 Methods

Constructor: Single parameter allows setting of global encoding for use by the current object.

__construct(string $encoding) 

If PHP 5.4 is detected, additional ENT_SUBSTITUTE flag is set for htmlspecialchars() calls.

Parameters

$encoding

string

Exceptions

\Zend\Escaper\Exception\InvalidArgumentException

Escape a string for the CSS context.

escapeCss(string $string) : string

CSS escaping can be applied to any string being inserted into CSS and escapes everything except alphanumerics.

Parameters

$string

string

Returns

string

Escape a string for the HTML Body context where there are very few characters of special meaning.

escapeHtml(string $string) : string

Internally this will use htmlspecialchars().

Parameters

$string

string

Returns

string

Escape a string for the HTML Attribute context.

escapeHtmlAttr(string $string) : string

We use an extended set of characters to escape that are not covered by htmlspecialchars() to cover cases where an attribute might be unquoted or quoted illegally (e.g. backticks are valid quotes for IE).

Parameters

$string

string

Returns

string

Escape a string for the Javascript context.

escapeJs(string $string) : string

This does not use json_encode(). An extended set of characters are escaped beyond ECMAScript's rules for Javascript literal string escaping in order to prevent misinterpretation of Javascript as HTML leading to the injection of special characters and entities. The escaping used should be tolerant of cases where HTML escaping was not applied on top of Javascript escaping correctly. Backslash escaping is not used as it still leaves the escaped character as-is and so is not useful in a HTML context.

Parameters

$string

string

Returns

string

Escape a string for the URI or Parameter contexts.

escapeUrl(string $string) : string

This should not be used to escape an entire URI - only a subcomponent being inserted. The function is a simple proxy to rawurlencode() which now implements RFC 3986 since PHP 5.3 completely.

Parameters

$string

string

Returns

string

Return the encoding that all output/input is expected to be encoded in.

getEncoding() : string

Returns

string

Encoding conversion helper which wraps iconv and mbstring where they exist or throws and exception where neither is available.

convertEncoding(string $string, string $to, array | string $from) : string

Parameters

$string

string

$to

string

$from

arraystring

Exceptions

\Zend\Escaper\Exception\RuntimeException

Returns

string

Callback function for preg_replace_callback that applies CSS escaping to all matches.

cssMatcher(array $matches) : string

Parameters

$matches

array

Returns

string

Converts a string from UTF-8 to the base encoding.

fromUtf8(string $string) : string

The base encoding is set via this class' constructor.

Parameters

$string

string

Returns

string

Callback function for preg_replace_callback that applies HTML Attribute escaping to all matches.

htmlAttrMatcher(array $matches) : string

Parameters

$matches

array

Returns

string

Checks if a given string appears to be valid UTF-8 or not.

isUtf8(string $string) : bool

Parameters

$string

string

Returns

bool

Callback function for preg_replace_callback that applies Javascript escaping to all matches.

jsMatcher(array $matches) : string

Parameters

$matches

array

Returns

string

Converts a string to UTF-8 from the base encoding.

toUtf8(string $string) : string

The base encoding is set via this class' constructor.

Parameters

$string

string

Exceptions

\Zend\Escaper\Exception\RuntimeException

Returns

string

 Properties

 

Static Matcher which escapes characters for CSS Attribute contexts

$cssMatcher : \Zend\Escaper\callable

 

Current encoding for escaping.

$encoding : string

If not UTF-8, we convert strings from this encoding pre-escaping and back to this encoding post-escaping.

 

Static Matcher which escapes characters for HTML Attribute contexts

$htmlAttrMatcher : \Zend\Escaper\callable

 

Entity Map mapping Unicode codepoints to any available named HTML entities.

$htmlNamedEntityMap : array

While HTML supports far more named entities, the lowest common denominator has become HTML5's XML Serialisation which is restricted to the those named entities that XML supports. Using HTML entities would result in this error: XML Parsing Error: undefined entity

 

Holds the value of the special flags passed as second parameter to htmlspecialchars().

$htmlSpecialCharsFlags : string

We modify these for PHP 5.4 to take advantage of the new ENT_SUBSTITUTE flag for correctly dealing with invalid UTF-8 sequences.

 

Static Matcher which escapes characters for Javascript contexts

$jsMatcher : \Zend\Escaper\callable

 

List of all encoding supported by this class

$supportedEncodings : array