wp_convert_bytes_to_hr()
云策文档标注
概述
wp_convert_bytes_to_hr() 是一个已弃用的 WordPress 函数,用于将整数字节值转换为简写的字节表示(如 KB、MB)。自 3.6.0 版本起,建议使用 size_format() 替代。
关键要点
- 函数 wp_convert_bytes_to_hr() 已弃用,从 WordPress 3.6.0 开始,推荐使用 size_format()。
- 参数 $bytes 为必需整数,表示字节值;返回简写字节字符串。
- 内部使用 _deprecated_function() 标记弃用,并基于 KB_IN_BYTES 常量计算单位。
代码示例
function wp_convert_bytes_to_hr( $bytes ) {
_deprecated_function( __FUNCTION__, '3.6.0', 'size_format()' );
$units = array( 0 => 'B', 1 => 'KB', 2 => 'MB', 3 => 'GB', 4 => 'TB' );
$log = log( $bytes, KB_IN_BYTES );
$power = ! is_nan( $log ) && ! is_infinite( $log ) ? (int) $log : 0;
$size = KB_IN_BYTES ** ( $log - $power );
if ( ! is_nan( $size ) && array_key_exists( $power, $units ) ) {
$unit = $units[ $power ];
} else {
$size = $bytes;
$unit = $units[0];
}
return $size . $unit;
}注意事项
由于此函数已弃用,开发者应避免在新代码中使用,并考虑迁移到 size_format() 以保持兼容性和最佳实践。
原文内容
Converts an integer byte value to a shorthand byte value.
Description
See also
Parameters
$bytesintrequired-
An integer byte value.
Source
function wp_convert_bytes_to_hr( $bytes ) {
_deprecated_function( __FUNCTION__, '3.6.0', 'size_format()' );
$units = array( 0 => 'B', 1 => 'KB', 2 => 'MB', 3 => 'GB', 4 => 'TB' );
$log = log( $bytes, KB_IN_BYTES );
$power = ! is_nan( $log ) && ! is_infinite( $log ) ? (int) $log : 0;
$size = KB_IN_BYTES ** ( $log - $power );
if ( ! is_nan( $size ) && array_key_exists( $power, $units ) ) {
$unit = $units[ $power ];
} else {
$size = $bytes;
$unit = $units[0];
}
return $size . $unit;
}
Changelog
| Version | Description |
|---|---|
| 3.6.0 | Deprecated. Use size_format() |
| 2.3.0 | Introduced. |