Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Console
  4. Helpers
  5. Formatter Helper

Formatter Helper

Edit this page

The Formatter helper provides functions to format the output with colors. You can do more advanced things with this helper than you can in How to Color and Style the Console Output.

The FormatterHelper is included in the default helper set and you can get it by calling getHelper():

1
$formatter = $this->getHelper('formatter');

The methods return a string, which you'll usually render to the console by passing it to the OutputInterface::writeln method.

Note

As an alternative, consider using the SymfonyStyle to display stylized blocks.

Print Messages in a Section

Symfony offers a defined style when printing a message that belongs to some "section". It prints the section in color and with brackets around it and the actual message to the right of this. Minus the color, it looks like this:

1
[SomeSection] Here is some message related to that section

To reproduce this style, you can use the formatSection() method:

1
2
3
4
5
$formattedLine = $formatter->formatSection(
    'SomeSection',
    'Here is some message related to that section'
);
$output->writeln($formattedLine);

Print Messages in a Block

Sometimes you want to be able to print a whole block of text with a background color. Symfony uses this when printing error messages.

If you print your error message on more than one line manually, you will notice that the background is only as long as each individual line. Use the formatBlock() to generate a block output:

1
2
3
$errorMessages = ['Error!', 'Something went wrong'];
$formattedBlock = $formatter->formatBlock($errorMessages, 'error');
$output->writeln($formattedBlock);

As you can see, passing an array of messages to the formatBlock() method creates the desired output. If you pass true as third parameter, the block will be formatted with more padding (one blank line above and below the messages and 2 spaces on the left and right).

The exact "style" you use in the block is up to you. In this case, you're using the pre-defined error style, but there are other styles (info, comment, question), or you can create your own. See How to Color and Style the Console Output.

Print Truncated Messages

Sometimes you want to print a message truncated to an explicit character length. This is possible with the truncate() method.

If you would like to truncate a very long message, for example, to 7 characters, you can write:

1
2
3
$message = "This is a very long message, which should be truncated";
$truncatedMessage = $formatter->truncate($message, 7);
$output->writeln($truncatedMessage);

And the output will be:

1
This is...

The message is truncated to the given length, then the suffix is appended to the end of that string.

Negative String Length

If the length is negative, the number of characters to truncate is counted from the end of the string:

1
$truncatedMessage = $formatter->truncate($message, -5);

This will result in:

1
This is a very long message, which should be trun...

Custom Suffix

By default, the ... suffix is used. If you wish to use a different suffix, pass it as the third argument to the method. The suffix is always appended, unless truncated length is longer than a message and a suffix length. If you don't want to use suffix at all, pass an empty string:

1
2
3
4
5
6
$truncatedMessage = $formatter->truncate($message, 7, '!!'); // result: This is!!
$truncatedMessage = $formatter->truncate($message, 7, '');   // result: This is

$truncatedMessage = $formatter->truncate('test', 10);
// result: test
// because length of the "test..." string is shorter than 10

Formatting Time

Sometimes you want to format seconds to time. This is possible with the formatTime() method. The first argument is the seconds to format and the second argument is the precision (default 1) of the result:

1
2
3
4
Helper::formatTime(42);        // 42 secs
Helper::formatTime(125);       // 2 mins
Helper::formatTime(125, 2);    // 2 mins, 5 secs
Helper::formatTime(172799, 4); // 1 day, 23 hrs, 59 mins, 59 secs

Formatting Memory

Sometimes you want to format memory to GiB, MiB, KiB and B. This is possible with the formatMemory() method. The only argument is the memory size to format:

1
2
3
4
Helper::formatMemory(512);                // 512 B
Helper::formatMemory(1024);               // 1 KiB
Helper::formatMemory(1024 * 1024);        // 1.0 MiB
Helper::formatMemory(1024 * 1024 * 1024); // 1 GiB
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 7.1 is backed by

    Online Sylius certification, take it now!

    Online Sylius certification, take it now!

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Version:

    Table of Contents

    • Print Messages in a Section
    • Print Messages in a Block
    • Print Truncated Messages
      • Negative String Length
      • Custom Suffix
    • Formatting Time
    • Formatting Memory

    Symfony footer

    Avatar of Michael Dwyer, a Symfony contributor

    Thanks Michael Dwyer (@kalifg) for being a Symfony contributor

    1 commit • 9 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony