How can I convert words to numbers in PHP?
The first method that springs to mind is reading each character and converting it to its ASCII value. Are there any other methods of doing this? I would like to be able to give a php script a string, and have it开发者_Python百科 turn that same string (that may or may not contain numbers or symbols) into the same series of numbers every time.
So, you're talking of hashing.
MD5() or (or sha1() as some local paranoids insists) can give you that number. Though a hexdecimal one. I hope it will fit your unclear goals.
This would work (but I'm not sure if I have fully grasped what you want to do):
function string2num($in_str) {
$out_str = '';
$chars = unpack('c*', $in_str);
foreach($chars as $char) {
$out_str .= $char;
}
return $out_str;
}
// Outputs:
// 8410410511532105115329732115116114105110103
$num = string2num('This is a string');
print "$num\n";
Maybe you want to read about type casting and juggling?
Can you provide some bounds for this function? Would it need to do fractions or decimals? Or numbers less than zero? How high would it need to go? English only?
精彩评论