如何将字符串转换为html颜色代码哈希? [英] How can I convert strings to an html color code hash?

查看:350
本文介绍了如何将字符串转换为html颜色代码哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串表示为任意html颜色。

I'd like to represent strings as arbitrary html colors.

示例:

=#FFCC00

foo foo 2=#565656

"blah blah" = #FFCC00
"foo foo 2" = #565656

无论实际的颜色代码是什么,一个有效的十六进制HTML颜色代码和整个频谱是相当好的表示。

It doesn't matter what the actual color code is, so long as it's a valid hexadecimal HTML color code and the whole spectrum is fairly well represented.

我想第一步是做一个MD5的字符串,然后以某种方式将其转换为十六进制颜色代码?

I guess the first step would be to do an MD5 on the string and then somehow convert that to hexadecimal color code?

更新:用法示例是在服务器上生成文件请求的可视报告。

Update: Usage example is to generate a visual report of file requests on a server. The colors don't have to look pretty, it's more so a human brain can detect patterns, etc in the data more readily.

推荐答案

function stringToColorCode($str) {
  $code = dechex(crc32($str));
  $code = substr($code, 0, 6);
  return $code;
}

$str = 'test123';
print '<span style="background-color:#'.stringToColorCode($str).'">'.$str.'</span>';

这篇关于如何将字符串转换为html颜色代码哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆