You are here

private static function BigPipe::splitHtmlOnPlaceholders in Drupal 9

Same name and namespace in other branches
  1. 8 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 768

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, NULL, $flags);
  }
  else {

    // For large amounts of placeholders we use a simpler but slower approach.
    foreach ($html_placeholders as $placeholder) {
      $html_string = str_replace($placeholder, "\37" . $placeholder . "\37", $html_string);
    }
    $result = array_filter(explode("\37", $html_string));
  }
  return $result;
}