Last active
December 14, 2017 21:19
-
-
Save KugiHaito/0562384845ce9c34226bde7130f35bfd to your computer and use it in GitHub Desktop.
Indenticar e substituir valores hexadecimal para "cor"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment