note 69978 modified in function.ord by danbrown

From: Date: Tue, 11 Oct 2011 16:08:43 +0000
Subject: note 69978 modified in function.ord by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183799@lists.php.net to get a copy of this message
Well, i was thinking about a method to hash a string with md5 in a loose way, so md5("HELLO") isn't the same like md5("Hello"), even, i my case, it is about cd-titles i got submitted by users. So i made some function transforming my string to right what i want Thisone is the "call" function returning the "loose hash". It will get only the chars of a string, make them to uppercase and then hash with md5. <?php function loosehash($string){ return md5(strtoupper(onlyChars($string))); } ?> Thisone is moving through a string like a chararray and check for the asciivalues, you can edit the values and condition to fit your needs <?php function onlyChars($string){ $strlength = strlen($string); $retString = ""; for($i = 0; $i < $strlength; $i++){ if((ord($string[$i]) >= 48 && ord($string[$i]) <= 57) || (ord($string[$i]) >= 65 && ord($string[$i]) <= 90) || (ord($string[$i]) >= 97 && ord($string[$i]) <= 122)){ $retString .= $string[$i]; } } echo $retString; } ?> --was-- Well, i was thinking about a method to hash a string with md5 in a loose way, so md5("HELLO") isn't the same like md5("Hello"), even, i my case, it is about cd-titles i got submitted by users. So i made some function transforming my string to right what i want Thisone is the "call" function returning the "loose hash". It will get only the chars of a string, make them to uppercase and then hash with md5. function loosehash($string){ return md5(strtoupper(onlyChars($string))); } Thisone is moving through a string like a chararray and check for the asciivalues, you can edit the values and condition to fit your needs function onlyChars($string){ $strlength = strlen($string); $retString = ""; for($i = 0; $i < $strlength; $i++){ if((ord($string[$i]) >= 48 && ord($string[$i]) <= 57) || (ord($string[$i]) >= 65 && ord($string[$i]) <= 90) || (ord($string[$i]) >= 97 && ord($string[$i]) <= 122)){ $retString .= $string[$i]; } } echo $retString; } http://php.net/manual/en/function.ord.php

« previous php.notes (#183799) next »