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!';
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 ;))
The colors and codes are ANSI escape sequences
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
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;}
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', '=');
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_padIf you want send ANSI color to console, get this tiny package,