1
0

bash-terminal-colors-to-html.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2013 Johannes 'Banana' Keßler
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  8. *
  9. * You should have received a copy of the
  10. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  11. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  12. */
  13. /**
  14. * convert bash terminal color codes to html element with a css class
  15. * works with this colour echo function: https://github.com/jumpin-banana/klimbim/blob/master/bash/colour-text-echo.sh
  16. * eg. the $string as a direct return from the system call which return a string formatted with the above
  17. * colour echo function.
  18. *
  19. * it can work with other functions but this is not tested.
  20. *
  21. * your can change the replace text with the html code you need. Also the css classes are needed.
  22. */
  23. function bashColortoHtml($string) {
  24. $ret = false;
  25. if(!empty($string)) {
  26. $_colorPattern = array(
  27. '/\\033\[1;33m(.*?)\\033\[0m/s',
  28. '/\\033\[0;31m(.*?)\\033\[0m/s',
  29. '/\\033\[0;34m(.*?)\\033\[0m/s',
  30. '/\\033\[0;36m(.*?)\\033\[0m/s',
  31. '/\\033\[0;35m(.*?)\\033\[0m/s',
  32. '/\\033\[0;33m(.*?)\\033\[0m/s',
  33. '/\\033\[1;37m(.*?)\\033\[0m/s',
  34. '/\\033\[0;30m(.*?)\\033\[0m/s',
  35. '/\\033\[0;32m(.*?)\\033\[0m/s'
  36. );
  37. $_colorReplace = array(
  38. '<span class="yellow">$1</span>',
  39. '<span class="red">$1</span>',
  40. '<span class="blue">$1</span>',
  41. '<span class="cyan">$1</span>',
  42. '<span class="purple">$1</span>',
  43. '<span class="brown">$1</span>',
  44. '<span class="white">$1</span>',
  45. '<span class="black">$1</span>',
  46. '<span class="green">$1</span>'
  47. );
  48. $ret = preg_replace($_colorPattern, $_colorReplace, $string);
  49. }
  50. return $ret;
  51. }
  52. ?>