Zend_Validate_StringLength encoding?
-
25-10-2019 - |
Question
In Zend Framework, Zend_Validate_StringLength
has an encoding property that can be used to convert between different character sets (it uses iconv_set_encoding
on the string).
Why is this part of the StringLength
validator class? Should I use this with my string length field checks to enforce UTF-8?
Solution
String lengths in PHP are the number of bytes taken to save the string. Some encodings use multiple bytes (e.g. two) to store single character.
Therefore, if you use encoding storing some of the characters in multiple bytes (e.g. Unicode UTF-8), you need to use this encoding to properly determine string length. If you use the default encoding settings, you may get inaccurate results.
However, I believe the default for all Zend components is UTF-8, which is the de facto standard in the web applications. Pity, this is not the default one for PHP (yet).
OTHER TIPS
Actually it uses iconv_set_encoding
only to test if current server supports specified $encoding
. The actual comparison is made with iconv_strlen()
.
Not sure I can get your second question. If you need to validate strings length - you need to use this validator.