문제

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?

도움이 되었습니까?

해결책

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).

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top