function BigPipe::splitHtmlOnPlaceholders

Same name and namespace in other branches
  1. 9 core/modules/big_pipe/src/Render/BigPipe.php \Drupal\big_pipe\Render\BigPipe::splitHtmlOnPlaceholders()
  2. 8.9.x core/modules/big_pipe/src/Render/BigPipe.php \Drupal\big_pipe\Render\BigPipe::splitHtmlOnPlaceholders()
  3. 10 core/modules/big_pipe/src/Render/BigPipe.php \Drupal\big_pipe\Render\BigPipe::splitHtmlOnPlaceholders()

Splits an HTML string into fragments.

Creates an array of HTML fragments, separated by placeholders. The result includes the placeholders themselves. The original order is respected.

Parameters

string $html_string: The HTML to split.

string[] $html_placeholders: The HTML placeholders to split on.

Return value

string[] The resulting HTML fragments.

1 call to BigPipe::splitHtmlOnPlaceholders()
BigPipe::sendNoJsPlaceholders in core/modules/big_pipe/src/Render/BigPipe.php
Sends no-JS BigPipe placeholders' replacements as embedded HTML responses.

File

core/modules/big_pipe/src/Render/BigPipe.php, line 699

Class

BigPipe
Service for sending an HTML response in chunks (to get faster page loads).

Namespace

Drupal\big_pipe\Render

Code

private static function splitHtmlOnPlaceholders($html_string, array $html_placeholders) {
    $prepare_for_preg_split = function ($placeholder_string) {
        return '(' . preg_quote($placeholder_string, '/') . ')';
    };
    $preg_placeholder_strings = array_map($prepare_for_preg_split, $html_placeholders);
    $pattern = '/' . implode('|', $preg_placeholder_strings) . '/';
    if (strlen($pattern) < 31000) {
        // Only small (<31K characters) patterns can be handled by preg_split().
        $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
        $result = preg_split($pattern, $html_string, 0, $flags);
    }
    else {
        // For large amounts of placeholders we use a simpler but slower approach.
        foreach ($html_placeholders as $placeholder) {
            $html_string = str_replace($placeholder, "\x1f" . $placeholder . "\x1f", $html_string);
        }
        $result = array_filter(explode("\x1f", $html_string));
    }
    return $result;
}

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