Guest User

Untitled

a guest
Dec 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Trocar valores hexadecimais por cor
  5. * @param string text
  6. * @return string
  7. *
  8. * @author Kugi Haito
  9. */
  10. function hexColor(string $text) {
  11. function getColorHex(string $text) {
  12. $letter = "";
  13. $colors = [];
  14. $positions = "";
  15. for($i=0; $i<strlen($text); $i++) {
  16. if ($text[$i] == "#"){
  17. $positions .= $i . ",";
  18. }
  19. }
  20. $p = explode(",", $positions);
  21. array_pop($p);
  22. foreach ($p as $key) {
  23. for ($j=intval($key); $j < intval($key)+7; $j++) {
  24. if ($text[$j] == "#" and $j > $key) {
  25. // blank
  26. } else {
  27. $letter .= $text[$j];
  28. }
  29. }
  30. array_push($colors, $letter);
  31. $letter = "";
  32.  
  33. }
  34. return $colors;
  35. }
  36.  
  37. $s_color = [];
  38. foreach (getColorHex($text) as $color) {
  39. array_push($s_color, "</span><span style='color:". $color .";'>");
  40. }
  41.  
  42. $textEdited = str_replace(
  43. array_values(getColorHex($text)),
  44. array_values($s_color),
  45. $text
  46. ) . "</span>";
  47. return $textEdited;
  48. }
  49.  
  50. // exemplo de entrada, e utilização..
  51. echo hexColor("#6bc609Olá #21b29cMundo!");
Add Comment
Please, Sign In to add comment