You are here

private function Application::splitStringByWidth in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/console/Application.php \Symfony\Component\Console\Application::splitStringByWidth()
1 call to Application::splitStringByWidth()
Application::renderException in vendor/symfony/console/Application.php
Renders a caught exception.

File

vendor/symfony/console/Application.php, line 1080

Class

Application
An Application is the container for a collection of commands.

Namespace

Symfony\Component\Console

Code

private function splitStringByWidth($string, $width) {

  // str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.
  // additionally, array_slice() is not enough as some character has doubled width.
  // we need a function to split string not by character count but by string width
  if (!function_exists('mb_strwidth')) {
    return str_split($string, $width);
  }
  if (false === ($encoding = mb_detect_encoding($string))) {
    return str_split($string, $width);
  }
  $utf8String = mb_convert_encoding($string, 'utf8', $encoding);
  $lines = array();
  $line = '';
  foreach (preg_split('//u', $utf8String) as $char) {

    // test if $char could be appended to current line
    if (mb_strwidth($line . $char, 'utf8') <= $width) {
      $line .= $char;
      continue;
    }

    // if not, push current line to array and make new line
    $lines[] = str_pad($line, $width);
    $line = $char;
  }
  if ('' !== $line) {
    $lines[] = count($lines) ? str_pad($line, $width) : $line;
  }
  mb_convert_variables($encoding, 'utf8', $lines);
  return $lines;
}