27

How do I change the color of an echo message and center the message in the PHP I've written. The line I have is:

echo 'Request has been sent. Please wait for my reply!';

mauris
  • 42,982
  • 15
  • 99
  • 131
The Woo
  • 17,809
  • 26
  • 57
  • 71

8 Answers8

134

How about writing out some escape sequences?

echo "\033[01;31m Request has been sent. Please wait for my reply! \033[0m";

Won't work through browser though, only from console ;))

kolypto
  • 31,774
  • 17
  • 105
  • 99
  • 3
    is there any documentation about this colors and options? – haynar Sep 28 '11 at 13:30
  • 5
    Here's the script that I use for this purpose, http://asheepapart.blogspot.com/2011/02/add-color-to-php-echo-in-cli.html – Ben Sep 04 '12 at 00:12
  • You also have to ensure that your terminal emulator is using a standard colour scheme, otherwise red may not display as red. – Damien Dec 08 '16 at 03:50
  • 1
    In case you're looking for a command line solution: https://github.com/box/bart/blob/master/src/Bart/EscapeColors.php Thanks to @Ben for the original link – Aternus Dec 26 '17 at 10:04
25

How about writing out some HTML tags and some CSS if you're outputting this to the browser?

echo '<span style="color:#AFA;text-align:center;">Request has been sent. Please wait for my reply!</span>';

Won't work from console though, only through browser.

mauris
  • 42,982
  • 15
  • 99
  • 131
3

And if you are using Command line on Windows, download a program ANSICON that enables console to accept color codes. ANSICON is available at https://github.com/adoxa/ansicon/releases

Lukasz Czerwinski
  • 13,499
  • 10
  • 55
  • 65
2

This is an old question, but no one responded to the question regarding centering text in a terminal.

/**
 * Centers a string of text in a terminal window
 *
 * @param string $text The text to center
 * @param string $pad_string If set, the string to pad with (eg. '=' for a nice header)
 *
 * @return string The padded result, ready to echo
 */
function center($text, $pad_string = ' ') {
    $window_size = (int) `tput cols`;
    return str_pad($text, $window_size, $pad_string, STR_PAD_BOTH)."\n";
}

echo center('foo');
echo center('bar baz', '=');
Mikkel
  • 1,192
  • 9
  • 22
  • Yes, just change `STR_PAD_BOTH` to `STR_PAD_LEFT` (because you're applying the padding on the left side to push the message to the right). See the [doc on `str_pad`](https://secure.php.net/str_pad) – Mikkel Sep 27 '17 at 14:51
2

this works for me every time try this.

echo "<font color='blue'>".$myvariable."</font>";

since font is not supported in html5 you can do this

echo "<p class="variablecolor">".$myvariable."</p>";

then in css do

.variablecolor{
color: blue;}
jerryurenaa
  • 3,863
  • 1
  • 27
  • 17
  • The tag is not supported in HTML5. Use CSS instead. – Jon B Oct 03 '19 at 03:36
  • you can do this in html5 and css echo "

    ".$myvariable."

    "; .variablecolor{ color: blue; }
    – jerryurenaa Oct 03 '19 at 04:37
  • I wasn't actually sure if terminals used html/CSS standards but I thought it was worth commenting. I've reverted my down vote though as it was a bad assumption sorry. – Jon B Oct 03 '19 at 08:12
1

If you want send ANSI color to console, get this tiny package,

http://pear.php.net/package/Console_Color

ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
1

Try this

<?php 
echo '<i style="color:blue;font-size:30px;font-family:calibri ;">
      hello php color </i> ';
//we cannot use double quote after echo , it must be single quote.
?>
Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
vithkimly
  • 11
  • 1
0

If it echoing out to a browser, you should use CSS. This would require also having the comment wrapped in an HTML tag. Something like:

echo '<p style="color: red; text-align: center">
      Request has been sent. Please wait for my reply!
      </p>';
Anthony
  • 36,459
  • 25
  • 97
  • 163