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

Breadcrumb

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

function AbstractString::wordwrap

File

vendor/symfony/string/AbstractString.php, line 658

Class

AbstractString
Represents a string of abstract characters.

Namespace

Symfony\Component\String

Code

public function wordwrap(int $width = 75, string $break = "\n", bool $cut = false) : static {
    $lines = '' !== $break ? $this->split($break) : [
        clone $this,
    ];
    $chars = [];
    $mask = '';
    if (1 === \count($lines) && '' === $lines[0]->string) {
        return $lines[0];
    }
    foreach ($lines as $i => $line) {
        if ($i) {
            $chars[] = $break;
            $mask .= '#';
        }
        foreach ($line->chunk() as $char) {
            $chars[] = $char->string;
            $mask .= ' ' === $char->string ? ' ' : '?';
        }
    }
    $string = '';
    $j = 0;
    $b = $i = -1;
    $mask = wordwrap($mask, $width, '#', $cut);
    while (false !== ($b = strpos($mask, '#', $b + 1))) {
        for (++$i; $i < $b; ++$i) {
            $string .= $chars[$j];
            unset($chars[$j++]);
        }
        if ($break === $chars[$j] || ' ' === $chars[$j]) {
            unset($chars[$j++]);
        }
        $string .= $break;
    }
    $str = clone $this;
    $str->string = $string . implode('', $chars);
    return $str;
}

API Navigation

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