CodeIgniter和utf8函数
My site needs to support multiple languages so it needs to handle utf8 data throughout the site. I understand how to configure codeigniter for utf8 data with regards to interacting with the database. My question is how does codeigniter handle things like rules for form fields when using utf8 encoding? For instance using strlen, for utf8 the strlen would be different and so you would need to use the mbstring function for that instead, mb_strlen. Does codeigniter have a setup that automatically handles that or is there something I need to configure inside codeigniter to do that?
我的网站需要支持多种语言,因此需要在整个网站上处理utf8数据。 我理解如何为与数据库交互的utf8数据配置codeigniter。 我的问题是codeigniter在使用utf8编码时如何处理表单字段规则之类的东西? 例如使用strlen,对于utf8,strlen会有所不同,所以你需要使用mbstring函数来代替mb_strlen。 codeigniter是否有一个自动处理的设置,或者我需要在codeigniter中配置一些东西吗? p> div>
It does use mb_strlen by default if it is accessible:
//below default min_length function of CI_Form_Validation lib as an example
/**
* Minimum Length
*
* @access public
* @param string
* @param value
* @return bool
*/
function min_length($str, $val)
{
if (preg_match("/[^0-9]/", $val))
{
return FALSE;
}
if (function_exists('mb_strlen'))
{
return (mb_strlen($str) < $val) ? FALSE : TRUE;
}
return (strlen($str) < $val) ? FALSE : TRUE;
}
For preg_match functions use unicode modifier:
// native CI alpha function:
function alpha($str)
{
return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
}
// ready for unicode using u modifier in regex pattern
function alpha($str)
{
return ( ! preg_match("/^([a-z])+$/iu", $str)) ? FALSE : TRUE;
}
No it doesn't, but you can extend the form validation class to use your own validation functions.