简体   繁体   中英

set global encoding in Zend Framework

My bootstrap sets encoding for all views:

protected function _initView () {
    $view = new Zend_View();
    // snip...
    $view->setEncoding('utf-8');
    // snip...
    return $view;
}

However, this does not set encoding for my form validators. The StringLength uses its default encoding (I'm not sure which that is) and it counts diacritics as two characters.

I know I can set the 'encoding' => 'utf-8' option when creating the validator, but it's kind of pesky to update all validators across my entire (huge) application. Is there a way to set the encoding for all validators at the same time?

You normally shouldn't have a problem with that if you've sent the Content-Type very early + defined it in the HTML-Document.

In PHP you can send the Encoding at the Entry-Point (which is in Zend Framework the "index.php"-File):

header( 'Content-Type: text/html; charset=utf-8' );

In your HTML-Layout you should place the META-Tag within your HEAD-Tag:

<!-- do this only in: HTML5 -->
<meta charset="utf-8" />

<!-- do this only in: HTML4 -->
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

And, of course, save every Source-File (HTML+PHP-Files) within the Project in UTF-8 (without BOM)

May be a bit late, but this would be the full answer:

mb_internal_encoding('utf-8');
iconv_set_encoding('internal_encoding', 'utf-8');

In ZF 1.1x the StringLength validator uses iconv_strlen :

int iconv_strlen(string $str [, string $charset = ini_set("iconv.internal_encoding")])

So one thing to try is to call ini_set (or iconv_set_encoding('internal_encoding', $encoding); ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM