You are here

public static function Bytes::validate in Drupal 9

Validate that a string is a representation of a number of bytes.

Parameters

string $string: The string to validate.

Return value

bool TRUE if the string is valid, FALSE otherwise.

2 calls to Bytes::validate()
BytesTest::testValidate in core/tests/Drupal/Tests/Component/Utility/BytesTest.php
Tests \Drupal\Component\Utility\Bytes::validate().
FileItem::validateMaxFilesize in core/modules/file/src/Plugin/Field/FieldType/FileItem.php
Form API callback.

File

core/lib/Drupal/Component/Utility/Bytes.php, line 116

Class

Bytes
Provides helper methods for byte conversions.

Namespace

Drupal\Component\Utility

Code

public static function validate($string) : bool {

  // Ensure that the string starts with a numeric character.
  if (!preg_match('/^[0-9]/', $string)) {
    return FALSE;
  }

  // Remove the numeric characters from the beginning of the value.
  $string = preg_replace('/^[0-9\\.]+/', '', $string);

  // Remove remaining spaces from the value.
  $string = trim($string);
  return in_array(strtolower($string), self::ALLOWED_SUFFIXES);
}