Home » Php » html – Any way to detect and disociate an HEX color code from a string in PHP?

html – Any way to detect and disociate an HEX color code from a string in PHP?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am making a server statistics page in PHP and one of my SQL query show the players nickname including his in-game HEX color codes.

e.g of what I get and what I actually want:

#FF0000Nick#00FF00name --> <span id="nickname"><span style="$color[0]">Nick</span><span style="$color[1]">name</span></span>

What i want to do is disociate the colors from the nickname so i can style them in css and make the nickname coloured.

Here is an idea of my actual code, it’s basically a table showing other informations but I’ll show you the one I use for the nickname:

while($row = mysqli_fetch_array($result)){

  $json = $row['data'];
  $playerDataTable = json_decode($json);

  foreach($playerDataTable as $playerData){

    $ingame_nickname = $playerData->nickname;

    echo "<div class='playerNames'";
        echo "<span>" . $forum_name . "</span>";
        echo "<span class='ingame_nickname'>" . $ingame_nickname . "</span>";
    echo "</div>";

  }

}

Thank you.

How to&Answers:

I’d suggest you use regular expressions:

$string = "#FF0000Nick#00FF00name";
$tokens = preg_split('/(#[A-Z0-9]{6})/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);

$tokens will be an array of 1, 3, or 5 elements; element 0 will be the part of the username before the first hex code, 1 the first hex code (including ‘#’) with 2 being the part of the text having that color, 3 the second hex code with 4 the part of the name colored like that.

If there’s 1 hex code only, $tokens will only contain 3 elements. If there isn’t any, $tokens will contain a single element, which is the full uncolored nickname.

echo $tokens[0];
for($x = 1; $x < count($tokens); $x = $x + 2) {
    $color = $tokens[$x];
    $name_token = $tokens[$x + 1];
    echo '<span style="color:' . $color . ';">' . $name_token . '</span>';
}

Answer:

You should really get your coder to make decent JSON. But however, you can use regex with named capture group

<?php

$json = '{ "nickname": "#FF0000Nick#00FF00name" }';
$data = json_decode($json, true);
$garbledData = $data['nickname'];

// (?<firstHex>\#[A-F0-9]{6})  match and name the match 'firstHex' - look for a hash, then letters a-f or numbers 0-9, with a length of 6 characters  

// (?<name>.+) match and name the match 'name' - look for absolutely any characters, any amount

// (?<secondHex>\#[A-F0-9]{6})  match and name the match 'secondHex' - look for a hash, then letters a-f or numbers 0-9, with a length of 6 characters 

$regex = '/(?<firstHex>\#[A-F0-9]{6})(?<name>.+)(?<secondHex>\#[A-F0-9]{6})/';
preg_match($regex, $garbledData, $matches);

$firstHex = $matches['firstHex'];
$secondHex = $matches['secondHex'];
$name = $matches['name'];

echo $firstHex . "\n";
echo $secondHex . "\n";
echo $name . "\n";

Output:

#FF0000 
#00FF00 
Nick

Check it out here https://3v4l.org/qo2gc