Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Trocar valores hexadecimais por cor
- * @param string text
- * @return string
- *
- * @author Kugi Haito
- */
- function hexColor(string $text) {
- function getColorHex(string $text) {
- $letter = "";
- $colors = [];
- $positions = "";
- for($i=0; $i<strlen($text); $i++) {
- if ($text[$i] == "#"){
- $positions .= $i . ",";
- }
- }
- $p = explode(",", $positions);
- array_pop($p);
- foreach ($p as $key) {
- for ($j=intval($key); $j < intval($key)+7; $j++) {
- if ($text[$j] == "#" and $j > $key) {
- // blank
- } else {
- $letter .= $text[$j];
- }
- }
- array_push($colors, $letter);
- $letter = "";
- }
- return $colors;
- }
- $s_color = [];
- foreach (getColorHex($text) as $color) {
- array_push($s_color, "</span><span style='color:". $color .";'>");
- }
- $textEdited = str_replace(
- array_values(getColorHex($text)),
- array_values($s_color),
- $text
- ) . "</span>";
- return $textEdited;
- }
- // exemplo de entrada, e utilização..
- echo hexColor("#6bc609Olá #21b29cMundo!");
Add Comment
Please, Sign In to add comment