Reputation: 24748
Let's say I have a string like this:
whateverthisis123 #_-
I want to convert this string into a number within a number interval forexample within 1-1000.
The above string could for example be converted into
387
This comes with a few rules:
Is there a built in function for this in PHP? If not, any clever idea how to create something like this?
Maybe it's easier if first converting in to MD5? http://www.php.net/manual/en/function.md5.php
Upvotes: 0
Views: 303
Reputation: 1099
Extended from my comment, here is a full code example (more fun way than just hash..)
<?php
function numberout(string $input)
{
$hash=md5($input);
$calculate=hexdec(substr($hash,0,3)); //take out 3 digits
$maxhex=4095; //3 digit hex ,65535 for 4 digit hex and so on...
$out = ($calculate*1000)/$maxhex;
return round($out);
}
?>
Sorry if this is programmartically wrong, I was used to c# and I haven't test this yet. So if there is any error I hope someone to edit it
Upvotes: 1
Reputation: 799
You're essentially describing a hash function. MD5 looks like the way to go. If you need to convert it to a number you could intval()
it. To keep it between 1-1000, use $number % 1000
.
Note: If this has to do with security/passwords, it's a bad idea.
Upvotes: 2