Bytes::toNumber($size) will throw warning is $size failed to parse

Created on 27 July 2020, over 4 years ago
Updated 10 June 2023, over 1 year ago

Problem/Motivation

If $size contains a comma, the \Drupal\Component\Utility\Bytes::toNumber($size) may return an unexpected value. The method removes commas.

public static function toNumber($size) {
  // Remove the non-unit characters from the size.
  $unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
  // Remove the non-numeric characters from the size.
  $size = preg_replace('/[^0-9\.]/', '', $size);

Steps to reproduce

var_dump(\Drupal\Component\Utility\Bytes::toNumber(2.5 MB)); // 2621440
var_dump(\Drupal\Component\Utility\Bytes::toNumber(2,5 MB)); // 26214400

Proposed resolution

Throw errors when there is something unexpected, e.g. commas ("2,5 MB").

Notice: \Drupal\Component\Utility\Bytes::toInt() is deprecated in favor of \Drupal\Component\Utility\Bytes::toNumber() β†’ .

πŸ“Œ Task
Status

Closed: duplicate

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 7 hours ago

Created by

πŸ‡΅πŸ‡±Poland Krzysztof DomaΕ„ski Poland

Live updates comments and jobs are added and updated live.
  • PHP 8.0

    The issue particularly affects sites running on PHP version 8.0.0 or later.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024