Converts a shorthand byte value to an integer byte value.
Parameters
$value
stringrequired- A (PHP ini) byte value, either shorthand or ordinary.
Source
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 );
}
Use size_format() to do the opposite (i.e., convert bytes to human-readable format).
<?php
$hr_bytes = size_format( 1024 ); // Returns '1 KiB'