wp_convert_hr_to_bytes()
Converts a shortened byte value to an integer byte value. For example, the string 1Mb will be converted to the number 1048576.
Function workflow:
- The passed value is processed through the functions trim() and strtolower().
- The value is "cleaned" separately using the intval() function, discarding letters.
- A check for the presence of characters such as
g,m, andkis performed, based on which multiplication occurs. - The resulting value is processed by the min() function.
size_format() — a similar function that, on the contrary, converts the passed number (bytes) into a readable form, for example, 9999 will turn into 10 KB.
Used By: wp_max_upload_size()
No Hooks.
Returns
Int. Size in bytes.
Usage
wp_convert_hr_to_bytes( $value );
- $value(string) (required)
- Byte value (PHP ini), shortened or regular. For example,
1Mbor20Kb.
Examples
#1 Examples of how the function works
wp_convert_hr_to_bytes('1Mb') //> 1048576
wp_convert_hr_to_bytes('1mb') //> 1048576
wp_convert_hr_to_bytes('1mB') //> 1048576
wp_convert_hr_to_bytes('1 Mb') //> 1048576
wp_convert_hr_to_bytes('1,9 Mb') //> 1048576
wp_convert_hr_to_bytes('1.9 Mb') //> 1048576
wp_convert_hr_to_bytes('15 Mb') //> 15728640
Changelog
| Since 2.3.0 | Introduced. |
| Since 4.6.0 | Moved from media.php to load.php. |
wp_convert_hr_to_bytes() wp convert hr to bytes code WP 6.9.1
function wp_convert_hr_to_bytes( $value ) {
$value = strtolower( trim( $value ) );
$bytes = (int) $value;
if ( str_contains( $value, 'g' ) ) {
$bytes *= GB_IN_BYTES;
} elseif ( str_contains( $value, 'm' ) ) {
$bytes *= MB_IN_BYTES;
} elseif ( str_contains( $value, 'k' ) ) {
$bytes *= KB_IN_BYTES;
}
// Deal with large (float) values which run into the maximum integer size.
return min( $bytes, PHP_INT_MAX );
}