PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

HtmlEntities

Returns the string $value, converting characters to their corresponding HTML entity equivalents where they exist.

Supported options for Zend_Filter_HtmlEntities

The following options are supported for Zend_Filter_HtmlEntities:

  • quotestyle: Equivalent to the PHP htmlentities native function parameter quote_style. This allows you to define what will be done with 'single' and "double" quotes. The following constants are accepted: ENT_COMPAT, ENT_QUOTES ENT_NOQUOTES with the default being ENT_COMPAT.

  • charset: Equivalent to the PHP htmlentities native function parameter charset. This defines the character set to be used in filtering. Unlike the PHP native function the default is 'UTF-8'. See "http://php.net/htmlentities" for a list of supported character sets.

    Note

    This option can also be set via the $options parameter as a Zend_Config object or array. The option key will be accepted as either charset or encoding.

  • doublequote: Equivalent to the PHP htmlentities native function parameter double_encode. If set to false existing html entities will not be encoded. The default is to convert everything (true).

    Note

    This option must be set via the $options parameter or the setDoubleEncode() method.

Basic usage

See the following example for the default behaviour of this filter.

<?php
$filter 
= new Zend_Filter_HtmlEntities();

print 
$filter->filter('<');

Quote Style

Zend_Filter_HtmlEntities allows changing the quote style used. This can be useful when you want to leave double, single, or both types of quotes un-filtered. See the following example:

<?php
$filter 
= new Zend_Filter_HtmlEntities(array('quotestyle' => ENT_QUOTES));

$input  "A 'single' and " '"double"';
print 
$filter->filter($input);

The above example returns A 'single' and "double". Notice that 'single' as well as "double" quotes are filtered.

<?php
$filter 
= new Zend_Filter_HtmlEntities(array('quotestyle' => ENT_COMPAT));

$input  "A 'single' and " '"double"';
print 
$filter->filter($input);

The above example returns A 'single' and "double". Notice that "double" quotes are filtered while 'single' quotes are not altered.

<?php
$filter 
= new Zend_Filter_HtmlEntities(array('quotestyle' => ENT_NOQUOTES));

$input  "A 'single' and " '"double"';
print 
$filter->filter($input);

The above example returns A 'single' and "double". Notice that neither "double" or 'single' quotes are altered.

Helper Methods

To change or retrieve the quotestyle after instantiation, the two methods setQuoteStyle() and getQuoteStyle() may be used respectively. setQuoteStyle() accepts one parameter $quoteStyle. The following constants are accepted: ENT_COMPAT, ENT_QUOTES, ENT_NOQUOTES

<?php
$filter 
= new Zend_Filter_HtmlEntities();

$filter->setQuoteStyle(ENT_QUOTES);
print 
$filter->getQuoteStyle(ENT_QUOTES);

To change or retrieve the charset after instantiation, the two methods setCharSet() and getCharSet() may be used respectively. setCharSet() accepts one parameter $charSet. See "http://php.net/htmlentities" for a list of supported character sets.

<?php
$filter 
= new Zend_Filter_HtmlEntities();

$filter->setQuoteStyle(ENT_QUOTES);
print 
$filter->getQuoteStyle(ENT_QUOTES);

To change or retrieve the doublequote option after instantiation, the two methods setDoubleQuote() and getDoubleQuote() may be used respectively. setDoubleQuote() accepts one boolean parameter $doubleQuote.

<?php
$filter 
= new Zend_Filter_HtmlEntities();

$filter->setQuoteStyle(ENT_QUOTES);
print 
$filter->getQuoteStyle(ENT_QUOTES);

Zend Framework