php - Using HTML Entities in a Zend Form Select


I am populating a Select form element, and if I try to use HTML Entities in the value, it get's converted, rather than displaying the special character.

This code:

$form->field_name->addMultiOption('value', ' • label');


<option value="one">&amp;nbsp;&amp;bull; label</option>

But I want it to be:

<option value="one">&nbsp;&bull; label</option>

How do I use HTML entities here?


I dug in the code and found that it's using theescape() function from the Zend View Abstract on the label AND the value. Maybe someone knows how to override/overload this function for a specific form element? I don't want to override that behavior by default.

Function from theZend_View_Helper_FormSelect class

protected function _build($value, $label, $selected, $disable)
    if (is_bool($disable)) {
        $disable = array();

    $opt = '<option'
         . ' value="' . $this->view->escape($value) . '"'
         . ' label="' . $this->view->escape($label) . '"';

    // selected?
    if (in_array((string) $value, $selected)) {
        $opt .= ' selected    }

    // disabled?
    if (in_array($value, $disable)) {
        $opt .= ' disabled    }

    $opt .= '>' . $this->view->escape($label) . "</option>";

    return $opt;

This is the function from theZend_View_Abstract class:

private $_escape = 'htmlspecialchars';

/* SNIP */

public function escape($var)
    if (in_array($this->_escape, array('htmlspecialchars', 'htmlentities'))) {
        return call_user_func($this->_escape, $var, ENT_COMPAT, $this->_encoding);

    return call_user_func($this->_escape, $var);



Turns out this isn't as complicated as I was making it.

I changed this:

$form->field_name->addMultiOption('value', '&nbsp;&bull; label');

To this:

    html_entity_decode('&nbsp;&bull;', ENT_COMPAT, 'UTF-8') . ' label');



You can try to switch off/clear Zend filters for specific fields when you populate them.

// pupulate the element 

When you clear the Zend filters, you can apply your own, prior populating.

People are also looking for solutions to the problem: php - How to integrate Flash and MySQL


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.