Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Bytes.php

function 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.

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

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal