兼容性函数

CodeIgniter 提供了一系列兼容性函数可以让你使用,它们只有在高版本的 PHP 中才有, 或者需要依赖其他的扩展才有。

由于是自己实现的,这些函数本身也可能有它自己的依赖性,但如果你的 PHP 中不提供这些函数时, 这些函数还是有用的。

Note

公用函数 <common_functions> 一样,兼容性函数也一直可以访问,只要满足了他们的依赖条件。

密码哈希

这几个兼容性函数移植了 PHP 标准的 密码哈希扩展 的实现, 这些函数只有在 PHP 5.5 以后的版本中才有。

依赖性

  • PHP 5.3.7
  • crypt() 函数需支持 CRYPT_BLOWFISH

常量

  • PASSWORD_BCRYPT
  • PASSWORD_DEFAULT

函数参考

password_get_info($hash)
Parameters:
  • $hash (string) – Password hash
Returns:

Information about the hashed password

Return type:

array

更多信息,请参考 PHP 手册中的 password_get_info() 函数

password_hash($password, $algo[, $options = array()])
Parameters:
  • $password (string) – Plain-text password
  • $algo (int) – Hashing algorithm
  • $options (array) – Hashing options
Returns:

Hashed password or FALSE on failure

Return type:

string

更多信息,请参考 PHP 手册中的 password_hash() 函数

Note

除非提供了你自己的有效盐值,该函数会依赖于一个可用的 CSPRNG 源(密码学安全的伪随机数生成器), 下面列表中的每一个都可以满足这点: - mcrypt_create_iv() with MCRYPT_DEV_URANDOM - openssl_random_pseudo_bytes() - /dev/arandom - /dev/urandom

password_needs_rehash()
Parameters:
  • $hash (string) – Password hash
  • $algo (int) – Hashing algorithm
  • $options (array) – Hashing options
Returns:

TRUE if the hash should be rehashed to match the given algorithm and options, FALSE otherwise

Return type:

bool

更多信息,请参考 PHP 手册中的 password_needs_rehash() 函数

password_verify($password, $hash)
Parameters:
  • $password (string) – Plain-text password
  • $hash (string) – Password hash
Returns:

TRUE if the password matches the hash, FALSE if not

Return type:

bool

更多信息,请参考 PHP 手册中的 password_verify() 函数

哈希(信息摘要)

兼容性函数移植了 hash_equals()hash_pbkdf2() 的实现, 这两函数分别在 PHP 5.6 和 PHP 5.5 以后的版本中才有。

函数参考

hash_equals($known_string, $user_string)
Parameters:
  • $known_string (string) – Known string
  • $user_string (string) – User-supplied string
Returns:

TRUE if the strings match, FALSE otherwise

Return type:

string

更多信息,请参考 PHP 手册中的 hash_equals() 函数

hash_pbkdf2($algo, $password, $salt, $iterations[, $length = 0[, $raw_output = FALSE]])
Parameters:
  • $algo (string) – Hashing algorithm
  • $password (string) – Password
  • $salt (string) – Hash salt
  • $iterations (int) – Number of iterations to perform during derivation
  • $length (int) – Output string length
  • $raw_output (bool) – Whether to return raw binary data
Returns:

Password-derived key or FALSE on failure

Return type:

string

更多信息,请参考 PHP 手册中的 hash_pbkdf2() 函数

多字节字符串

这一系列兼容性函数提供了对 PHP 的 多字节字符串扩展 的有限支持, 由于可选的解决方法有限,所以只有几个函数是可用的。

Note

如果没有指定字符集参数,默认使用 $config['charset'] 配置。

依赖性

Important

这个依赖是可选的,无论 iconv 扩展是否存在,这些函数都已经定义了, 如果 iconv 扩展不可用,它们会降级到非多字节字符串的函数版本。

Important

当设置了字符集时,该字符集必须被 iconv 支持,并且要设置成它可以识别的格式。

Note

如果你需要判断是否支持真正的多字节字符串扩展,可以使用 MB_ENABLED 常量。

函数参考

mb_strlen($str[, $encoding = NULL])
Parameters:
  • $str (string) – Input string
  • $encoding (string) – Character set
Returns:

Number of characters in the input string or FALSE on failure

Return type:

string

更多信息,请参考 PHP 手册中的 mb_strlen() 函数

mb_strpos($haystack, $needle[, $offset = 0[, $encoding = NULL]])
Parameters:
  • $haystack (string) – String to search in
  • $needle (string) – Part of string to search for
  • $offset (int) – Search offset
  • $encoding (string) – Character set
Returns:

Numeric character position of where $needle was found or FALSE if not found

Return type:

mixed

更多信息,请参考 PHP 手册中的 mb_strpos() 函数

mb_substr($str, $start[, $length = NULL[, $encoding = NULL]])
Parameters:
  • $str (string) – Input string
  • $start (int) – Position of first character
  • $length (int) – Maximum number of characters
  • $encoding (string) – Character set
Returns:

Portion of $str specified by $start and $length or FALSE on failure

Return type:

string

更多信息,请参考 PHP 手册中的 mb_substr() 函数

标准函数

这一系列兼容性函数提供了一些高版本的 PHP 中才有的标准函数。

函数参考

array_column(array $array, $column_key[, $index_key = NULL])
Parameters:
  • $array (array) – Array to fetch results from
  • $column_key (mixed) – Key of the column to return values from
  • $index_key (mixed) – Key to use for the returned values
Returns:

An array of values representing a single column from the input array

Return type:

array

更多信息,请参考 PHP 手册中的 array_column() 函数

array_replace(array $array1[, ...])
Parameters:
  • $array1 (array) – Array in which to replace elements
  • ... (array) – Array (or multiple ones) from which to extract elements
Returns:

Modified array

Return type:

array

更多信息,请参考 PHP 手册中的 array_replace() 函数

array_replace_recursive(array $array1[, ...])
Parameters:
  • $array1 (array) – Array in which to replace elements
  • ... (array) – Array (or multiple ones) from which to extract elements
Returns:

Modified array

Return type:

array

更多信息,请参考 PHP 手册中的 array_replace_recursive() 函数

Important

只有 PHP 原生的函数才可以检测到无穷递归,如果你使用的是 PHP 5.3+ ,使用时要担心引用!

hex2bin($data)
Parameters:
  • $data (array) – Hexadecimal representation of data
Returns:

Binary representation of the given data

Return type:

string

更多信息,请参考 PHP 手册中的 hex2bin() 函数

quoted_printable_encode($str)
Parameters:
  • $str (string) – Input string
Returns:

8bit-encoded string

Return type:

string

更多信息,请参考 PHP 手册中的 quoted_printable_encode() 函数