Skip to content

Instantly share code, notes, and snippets.

@KugiHaito
Last active December 14, 2017 21:19
Show Gist options
  • Save KugiHaito/0562384845ce9c34226bde7130f35bfd to your computer and use it in GitHub Desktop.
Save KugiHaito/0562384845ce9c34226bde7130f35bfd to your computer and use it in GitHub Desktop.
Indenticar e substituir valores hexadecimal para "cor"
<?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