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

Breadcrumb

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

function Regex::getHtmlPattern

Converts the htmlPattern to a suitable format for HTML5 pattern. Example: /^[a-z]+$/ would be converted to [a-z]+ However, if options are specified, it cannot be converted.

See also

http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute

File

vendor/symfony/validator/Constraints/Regex.php, line 90

Class

Regex
Validates that a value matches a regular expression.

Namespace

Symfony\Component\Validator\Constraints

Code

public function getHtmlPattern() : ?string {
    // If htmlPattern is specified, use it
    if (null !== $this->htmlPattern) {
        return $this->htmlPattern ?: null;
    }
    // Quit if delimiters not at very beginning/end (e.g. when options are passed)
    if ($this->pattern[0] !== $this->pattern[\strlen($this->pattern) - 1]) {
        return null;
    }
    $delimiter = $this->pattern[0];
    // Unescape the delimiter
    $pattern = str_replace('\\' . $delimiter, $delimiter, substr($this->pattern, 1, -1));
    // If the pattern is inverted, we can wrap it in
    // ((?!pattern).)*
    if (!$this->match) {
        return '((?!' . $pattern . ').)*';
    }
    // If the pattern contains an or statement, wrap the pattern in
    // .*(pattern).* and quit. Otherwise we'd need to parse the pattern
    if (str_contains($pattern, '|')) {
        return '.*(' . $pattern . ').*';
    }
    // Trim leading ^, otherwise prepend .*
    $pattern = '^' === $pattern[0] ? substr($pattern, 1) : '.*' . $pattern;
    // Trim trailing $, otherwise append .*
    return '$' === $pattern[\strlen($pattern) - 1] ? substr($pattern, 0, -1) : $pattern . '.*';
}

API Navigation

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