DWilliams Posted February 3, 2010 Share Posted February 3, 2010 So long story short I have a fairly complicated PHP CLI program going here. Part of the input it receives from users naturally has Linux terminal codes. I want to log input from users however I don't want to output these colors to the logs because it will just end up as gibberish in the log file. I have my colors defined as follows (and it works fine, I use it for outputting colored text at the terminal): $C['NORMAL'] = chr(27).'[0m'; $C['BOLD'] = chr(27).'[1m'; $C['LIGHT_BLACK'] = chr(27).'[1;30m'; $C['BLACK'] = chr(27).'[0;30m'; $C['LIGHT_RED'] = chr(27).'[1;31m'; $C['RED'] = chr(27).'[0;31m'; $C['LIGHT_GREEN'] = chr(27).'[1;32m'; $C['GREEN'] = chr(27).'[0;32m'; $C['LIGHT_YELLOW'] = chr(27).'[1;33m'; $C['YELLOW'] = chr(27).'[0;33m'; $C['LIGHT_BLUE'] = chr(27).'[1;34m'; $C['BLUE'] = chr(27).'[0;34m'; $C['LIGHT_MAGENTA'] = chr(27).'[1;35m'; $C['MAGENTA'] = chr(27).'[0;35m'; $C['LIGHT_CYAN'] = chr(27).'[1;36m'; $C['CYAN'] = chr(27).'[0;36m'; $C['WHITE'] = chr(27).'[1;37m'; $C['GREY'] = chr(27).'[0;37m'; Now, I figured it would be simple to write a function to strip colors from a string. Apparently I was wrong. I wrote the following: function StripColors($str) { global $C; foreach($C as $color) $str = str_replace($color, '', $str); return $str; } Unfortunately it appears to return the passed string completely unmodified. Help? Link to comment https://forums.phpfreaks.com/topic/190742-stripping-terminal-color-codes-in-a-cli-script/ Share on other sites More sharing options...
JasonLewis Posted February 3, 2010 Share Posted February 3, 2010 Try running some debugging: function StripColors($str){ global $C; foreach($C as $color){ if(strpos($str, $color) === false) echo "Could not find {$color}.<br />"; }else{ echo "Found {$color} in string.<br />"; } } } Just see if it is even finding the colors. Link to comment https://forums.phpfreaks.com/topic/190742-stripping-terminal-color-codes-in-a-cli-script/#findComment-1005906 Share on other sites More sharing options...
DWilliams Posted February 4, 2010 Author Share Posted February 4, 2010 Running that confirms that it isn't making a match on the color. I thought the problem might be in that I used an associative array for colors, so I changed the foreach line to "foreach(array_values($C) as $color", but nothing appears to have changed. Maybe the escape sequence in the colors (chr(27)) is messing up the match? Link to comment https://forums.phpfreaks.com/topic/190742-stripping-terminal-color-codes-in-a-cli-script/#findComment-1006523 Share on other sites More sharing options...
DWilliams Posted February 5, 2010 Author Share Posted February 5, 2010 Bump Link to comment https://forums.phpfreaks.com/topic/190742-stripping-terminal-color-codes-in-a-cli-script/#findComment-1007156 Share on other sites More sharing options...
DWilliams Posted February 5, 2010 Author Share Posted February 5, 2010 Bump. Perhaps I should have tried a more generic topic name as to not scare people away? I'm pretty sure the problem is something unrelated to terminal codes and CLI scripts. Is there some sort of "gotcha" with str_replace and escape characters? Link to comment https://forums.phpfreaks.com/topic/190742-stripping-terminal-color-codes-in-a-cli-script/#findComment-1007681 Share on other sites More sharing options...
DWilliams Posted February 7, 2010 Author Share Posted February 7, 2010 Bump again Link to comment https://forums.phpfreaks.com/topic/190742-stripping-terminal-color-codes-in-a-cli-script/#findComment-1008247 Share on other sites More sharing options...
DWilliams Posted February 7, 2010 Author Share Posted February 7, 2010 Bump again. Maybe I should remake this topic with a more general question. Link to comment https://forums.phpfreaks.com/topic/190742-stripping-terminal-color-codes-in-a-cli-script/#findComment-1008496 Share on other sites More sharing options...
teamatomic Posted February 7, 2010 Share Posted February 7, 2010 I havent replied cause I havent played with color codes since I pulled my BBS in '94, did you try a straight escape?$C['NORMAL'] = "\\.'[0m'"; or maybe a preg using perl escape \e. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/190742-stripping-terminal-color-codes-in-a-cli-script/#findComment-1008499 Share on other sites More sharing options...
DWilliams Posted February 10, 2010 Author Share Posted February 10, 2010 I havent replied cause I havent played with color codes since I pulled my BBS in '94, did you try a straight escape?$C['NORMAL'] = "\\.'[0m'"; or maybe a preg using perl escape \e. HTH Teamatomic I'll try that when I get home but it shouldn't make much of a difference, should it? The codes work fine when outputting, I just can't match on them. Link to comment https://forums.phpfreaks.com/topic/190742-stripping-terminal-color-codes-in-a-cli-script/#findComment-1010243 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.