benphelps Posted August 13, 2009 Share Posted August 13, 2009 I have checked everywhere I know on the interwebs looking for a function to color the CMD line output. I know it can be done, I have done it before, but I lost my snippets folder and now I need the function. Does anyone here on PHPFreaks have this function, or know how it can be done? It wasn't alot of code, maybe 15 lines in total. Quote Link to comment https://forums.phpfreaks.com/topic/170163-solved-color-the-cmd-line/ Share on other sites More sharing options...
benphelps Posted August 14, 2009 Author Share Posted August 14, 2009 I don't usually bump things, but I really really need this function. With limited knowledge of the Linux CMD line, I have no idea where to start. If someone could help me, I can offer (good) hosting (Web or VPS) in exchange. (if me offering something in return is out of the TOS for this forum category, feel free to move it) Quote Link to comment https://forums.phpfreaks.com/topic/170163-solved-color-the-cmd-line/#findComment-897794 Share on other sites More sharing options...
kenrbnsn Posted August 14, 2009 Share Posted August 14, 2009 I don't even know what you're asking, so I can begin to tell you an answer. Please explain it more (and better). Ken Quote Link to comment https://forums.phpfreaks.com/topic/170163-solved-color-the-cmd-line/#findComment-897871 Share on other sites More sharing options...
benphelps Posted August 14, 2009 Author Share Posted August 14, 2009 I'm not sure how I could explain it any better. Instead of plain white text in the command line, color the output. I start a php script from the command line that prints out some text, I would like to color that text. I have done this before with a simple function, maybe 15 lines of code max. I have just lost my snippets folder, so in that, I lost the function. I found the function on the a blog, which I found via. StumbleUpon. I have tried (for days now) to find this snippet, or any other snippet that might work with no luck. Quote Link to comment https://forums.phpfreaks.com/topic/170163-solved-color-the-cmd-line/#findComment-897898 Share on other sites More sharing options...
JonnoTheDev Posted August 14, 2009 Share Posted August 14, 2009 Heres the colour codes http://www.linux.com/archive/articles/113860 $output = "Hello World\n"; print "\033[31m".$output; Should be really easy to write a function. I use the above to print red text. I then use the following function to clear the screen so you get a blink effect when a process is going through a loop i.e. Cleaning database records. function clearscreen() { $clearscreen = chr(27)."[H".chr(27)."[2J"; print $clearscreen; } Quote Link to comment https://forums.phpfreaks.com/topic/170163-solved-color-the-cmd-line/#findComment-897984 Share on other sites More sharing options...
benphelps Posted August 14, 2009 Author Share Posted August 14, 2009 THANK YOU! Here is the function I wrote to make it easy. I'm sure this can be compressed down to a smaller function, but it does the job for me. function cmdColor($text, $color = "32"){ $color = strtolower($color); switch($color){ case "30": case "black": $prefx = "\033[30m"; break; case "31": case "red": $prefx = "\033[31m"; break; case "32": case "green": $prefx = "\033[32m"; break; case "33": case "yellow": $prefx = "\033[33m"; break; case "34": case "blue": $prefx = "\033[34m"; break; case "35": case "magenta": $prefx = "\033[35m"; break; case "36": case "cyan": $prefx = "\033[36m"; break; case "37": case "white": $prefx = "\033[37m"; break; } print $prefx.$text."\33[0m"; } I added "\33[0m" to the end so it sets the color back to default, this way, if the script terms, your not stuck in a colored cmd line. Quote Link to comment https://forums.phpfreaks.com/topic/170163-solved-color-the-cmd-line/#findComment-898038 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.