![]() |
|
Snippets |
|
Scriptaculous sends the request parameters always in UTF-8 encoding, regardless of what the page encoding has been set to. This can cause problems with special characters like german umlauts if your general characterset is not set to utf8.
The filter below acts on ajax requests only, converting the parameter values to single-byte ISO-8859-1.
You can enable this filter in your filters.yml by adding the following:
xmlhttpUtf8DecodeFilter:
class: xmlhttpUtf8DecodeFilter
Below is the code of the actual filter. Store this in the lib directory of your project/application (and do not forget to clean your cache: symfony cc).
<?php /** * This filter acts on AJAX requests and decodes the * request parameter values using the php utf8_decode() * function. * * @author jmunz <jochen.munz@virn.de> */ class xmlhttpUtf8DecodeFilter extends sfFilter { /** * Run the filter */ public function execute ($filterChain) { $request = $this->getContext()->getRequest(); // Only act if this is an ajax request if ( $request->isXmlHttpRequest() && $this->isFirstCall() ){ $params = $request->getParameterHolder()->getAll(); $this->decodeParameters($params); } // execute next filter $filterChain->execute(); } /** * Decode parameters */ private function decodeParameters(&$params){ foreach (array_keys($params) as $key) { if ( is_array($params[$key]) ) { $this->decodeParameters($params[$key]); } else { $params[$key] = utf8_decode($params[$key]); } } } }
Comments on this snippet
It should be noted that this will cause a problem if the request includes characters that are outside the iso-88591 charset.
I would suggest that websites should always be developed using UTF-8 as you would hope the developers are planning to create a popular application that people may wish to outside of the western world?
Vielen dank Jochen,
(die/das/der?) snippet helft mir!
Thank you so much