Symfony\Component\Console\Helper\Helper::strlenWithoutDecoration PHP Method

strlenWithoutDecoration() public static method

public static strlenWithoutDecoration ( Symfony\Component\Console\Formatter\OutputFormatterInterface $formatter, $string )
$formatter Symfony\Component\Console\Formatter\OutputFormatterInterface
    public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, $string)
    {
        $isDecorated = $formatter->isDecorated();
        $formatter->setDecorated(false);
        // remove <...> formatting
        $string = $formatter->format($string);
        // remove already formatted characters
        $string = preg_replace("/\\[[^m]*m/", '', $string);
        $formatter->setDecorated($isDecorated);
        return self::strlen($string);
    }

Usage Example

示例#1
0
 /**
  * Formats a message as a block of text.
  *
  * @param string|array $messages The message to write in the block
  * @param string|null $type The block type (added in [] on first line)
  * @param string|null $style The style to apply to the whole block
  * @param string $prefix The prefix for the block
  * @param bool $padding Whether to add vertical padding
  */
 public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false)
 {
     $this->autoPrependBlock();
     $messages = is_array($messages) ? array_values($messages) : array($messages);
     $lines = array();
     // add type
     if (null !== $type) {
         $messages[0] = sprintf('[%s] %s', $type, $messages[0]);
     }
     // wrap and add newlines for each element
     foreach ($messages as $key => $message) {
         $message = OutputFormatter::escape($message);
         $lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen($prefix), PHP_EOL, true)));
         if (count($messages) > 1 && $key < count($messages) - 1) {
             $lines[] = '';
         }
     }
     if ($padding && $this->isDecorated()) {
         array_unshift($lines, '');
         $lines[] = '';
     }
     foreach ($lines as &$line) {
         $line = sprintf('%s%s', $prefix, $line);
         $line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line));
         if ($style) {
             $line = sprintf('<%s>%s</>', $style, $line);
         }
     }
     $this->writeln($lines);
     $this->newLine();
 }
All Usage Examples Of Symfony\Component\Console\Helper\Helper::strlenWithoutDecoration