【发布时间】:2014-11-21 07:29:00
【问题描述】:
嗨,我有如下字符串
$string = "hello this is big test string"
或
$string = "hello small";
我需要从上面的字符串中生成唯一的 10 个数字。它不必是可逆的,但应该始终生成相同的唯一 10 个数字。
我尝试了什么:
function toNumber($dest)
{
if ($dest)
return ord(strtolower($dest)) - 96;
else
return 0;
}
$str = "hello this is string";
$chars = str_split($str);
foreach($chars as $char){
echo toNumber($char) . " ";
}
附言
- 对于小于 10 个字符的较小字符串也应该生成 10 个随机数
- 我有庞大的字符串数据库,所以数字不应该依赖于第一个或最后 10 个字符。它应该是所有单词的混合,并且尽可能唯一。
谢谢大家。
【问题讨论】:
-
我在回答中添加了更多解释。