function Bytes::validate

Same name in other branches
  1. 9 core/lib/Drupal/Component/Utility/Bytes.php \Drupal\Component\Utility\Bytes::validate()
  2. 11.x core/lib/Drupal/Component/Utility/Bytes.php \Drupal\Component\Utility\Bytes::validate()

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.

3 calls to Bytes::validate()
Bytes::validateConstraint in core/lib/Drupal/Component/Utility/Bytes.php
Validates a string is a representation of a number of bytes.
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 99

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.