wp_convert_hr_to_bytes()WP 2.3.0

Converts a shorthand byte value to an integer byte value.

No Hooks.

Return

Int. An integer byte value.

Usage

wp_convert_hr_to_bytes( $value );
$value(string) (required)
A (PHP ini) byte value, either shorthand or ordinary.

Examples

0

#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() code WP 6.6

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 );
}