Symfony Console Escape exit

I am generating a message with sprintf() , which will then be output using the Symfony Console Component in color:

 $mask = '<info>%s</info>'; $message = sprintf($mask, 'MyString'); $output->writeln($message); 

This usually works (displays the namespace in green). However, if the line ends with a backslash , the closing information tag is ignored:

 $message = sprintf($mask, 'MyString\'); $output->writeln($message); 

Output:

 MyString</info> ^^^^^^^ 

Obviously, the backslash seems to be some kind of escape character, but how to avoid this? Or how to save the value of the closing tag </info> ?

So far I have tried:

  • addcslashes('My\String\', '\\') - duplicates inside and one-fy at the end:

     My\\String\</info> 
  • &#92; as an HTML entity, a sequence of HTML objects is simply displayed verbatim, and there is no closing tag:

     My&#92;String&#92; 
+5
source share
1 answer

The character < can be avoided with \ , you guessed it. And the backslash can be escaped from Symfony v3.0.3, v2.8.3, v2.7.10 and v2.3.38 with OutputFormatter :

 use Symfony\Component\Console\Formatter\OutputFormatter; $mask = '<info>%s</info>'; $message = sprintf($mask, OutputFormatter::escape('MyString\\')); $output->writeln($message); 

Otherwise, you can use:

 $mask = "\033[32m%s\033[0m"; $message = sprintf($mask, 'MyString\\'); $output->writeln($message); 
+3
source

Source: https://habr.com/ru/post/1247025/


All Articles