Related
I want to shorten long integers in PHP to a string in the same way as how hexadecimal works. I think Google also use this for there sort urls in YouTube.
I have a string of numbers and letters that i want to use:
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
where:
8 = 8
a = 10
f = 15
Z = 61
10 = 62
11 = 63
1a = 72
and zo on...
the purpose is to set an integer the sortest way in a parameter in an affiliate url subid for tracking.
I was looking for a good name to discripe my question i find out that there is a sexagesimal numeral system that uses sixty as its base.
Here you can find a function that is shorten urls and avoid fonfusion between similar characters like 0/o and i/1/l/L etc.
<?php
/**
* A pure PHP implementation of NewBase60. A base 60 numbering system designed for
* use with URL shortening. Limited/overlapping character set to avoid confusion
* between similar characters, eg o/0, i/l, etc.
*
* Q: Why not use PHP base_convert()?
* A: Because it only goes up to base 36, and doesn't support the NewBase60
*
* #see http://tantek.pbworks.com/w/page/19402946/NewBase60
* #see http://en.wikipedia.org/wiki/sexagesimal
*
* #author david#dsingleton.co.uk
*/
class NewBase60
{
protected static $characterSet = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz';
/**
* Convert a sexagesimal number to a decimal number
* #param string sexagesimal number to convert
* #return integer Decimal representation of sexagesimal number
*/
public static function toDecimal($sexNum)
{
// Return falsy and 0 values as is
if (!$sexNum) {
return $sexNum === '0' ? 0 : $sexNum;
}
$decNum = 0;
foreach(str_split($sexNum) as $chr) {
$ord = ord($chr);
if ($ord>=48 && $ord<=57) { $ord -= 48; } // 0 - 9
elseif ($ord>=65 && $ord<=72) { $ord -= 55; } // A - H
elseif ($ord==73 || $ord==108) { $ord = 1; } // Error correct typo: capital I, lowercase l to 1
elseif ($ord>=74 && $ord<=78) { $ord -= 56; } // J - N
elseif ($ord==79) { $ord = 0; } // Error correct typo: capital O to 0
elseif ($ord>=80 && $ord<=90) { $ord -= 57; } // P - Z
elseif ($ord==95) { $ord = 34; } // underscore
elseif ($ord>=97 && $ord<=107) { $ord -= 62; } // a - k
elseif ($ord>=109 && $ord<=122) { $ord -= 63; } // m - z
else { $ord = 0; } // treat all other noise as 0
$decNum = 60 *$decNum + $ord;
}
return $decNum;
}
/**
* Convert a decimal number to a sexagesimal number
* #param integer Decimal number to convert
* #return string sexagesimal representation of decimal
*/
public static function fromDecimal($decNum)
{
$decNum = (int) $decNum;
if (!$decNum) {
return $decNum === 0 ? '0' : $sexNum;
}
$aSexCharset = self::$characterSet;
$result = '';
while ($decNum > 0) {
$decRemainder = $decNum % 60;
$decNum = ($decNum - $decRemainder) / 60;
$result = $aSexCharset[$decRemainder] . $result;
}
return $result;
}
}
Copyright: https://github.com/dsingleton/new-base-60/blob/master/newbase60.class.php
I'm asking how to convert KB MB GB TB & co. into bytes.
For example:
byteconvert("10KB") // => 10240
byteconvert("10.5KB") // => 10752
byteconvert("1GB") // => 1073741824
byteconvert("1TB") // => 1099511627776
and so on...
EDIT: wow. I've asked this question over 4 years ago. Thise kind of things really show you how much you've improved over time!
Here's a function to achieve this:
function convertToBytes(string $from): ?int {
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
$number = substr($from, 0, -2);
$suffix = strtoupper(substr($from,-2));
//B or no suffix
if(is_numeric(substr($suffix, 0, 1))) {
return preg_replace('/[^\d]/', '', $from);
}
$exponent = array_flip($units)[$suffix] ?? null;
if($exponent === null) {
return null;
}
return $number * (1024 ** $exponent);
}
$testCases = ["13", "13B", "13KB", "10.5KB", "123Mi"];
var_dump(array_map('convertToBytes', $testCases));
Output:
array(5) { [0]=> int(13) [1]=> int(13) [2]=> int(13312) [3]=>
int(10752) [4]=> NULL } int(1)
function toByteSize($p_sFormatted) {
$aUnits = array('B'=>0, 'KB'=>1, 'MB'=>2, 'GB'=>3, 'TB'=>4, 'PB'=>5, 'EB'=>6, 'ZB'=>7, 'YB'=>8);
$sUnit = strtoupper(trim(substr($p_sFormatted, -2)));
if (intval($sUnit) !== 0) {
$sUnit = 'B';
}
if (!in_array($sUnit, array_keys($aUnits))) {
return false;
}
$iUnits = trim(substr($p_sFormatted, 0, strlen($p_sFormatted) - 2));
if (!intval($iUnits) == $iUnits) {
return false;
}
return $iUnits * pow(1024, $aUnits[$sUnit]);
}
Here's what I've come up with so far, that I think is a much more elegant solution:
/**
* Converts a human readable file size value to a number of bytes that it
* represents. Supports the following modifiers: K, M, G and T.
* Invalid input is returned unchanged.
*
* Example:
* <code>
* $config->human2byte(10); // 10
* $config->human2byte('10b'); // 10
* $config->human2byte('10k'); // 10240
* $config->human2byte('10K'); // 10240
* $config->human2byte('10kb'); // 10240
* $config->human2byte('10Kb'); // 10240
* // and even
* $config->human2byte(' 10 KB '); // 10240
* </code>
*
* #param number|string $value
* #return number
*/
public function human2byte($value) {
return preg_replace_callback('/^\s*(\d+)\s*(?:([kmgt]?)b?)?\s*$/i', function ($m) {
switch (strtolower($m[2])) {
case 't': $m[1] *= 1024;
case 'g': $m[1] *= 1024;
case 'm': $m[1] *= 1024;
case 'k': $m[1] *= 1024;
}
return $m[1];
}, $value);
}
I use a function to determine the memory limit set for PHP in some cron scripts that looks like:
$memoryInBytes = function ($value) {
$unit = strtolower(substr($value, -1, 1));
return (int) $value * pow(1024, array_search($unit, array(1 =>'k','m','g')));
}
A similar approach that will work better with floats and accept the two letter abbreviation would be something like:
function byteconvert($value) {
preg_match('/(.+)(.{2})$/', $value, $matches);
list($_,$value,$unit) = $matches;
return (int) ($value * pow(1024, array_search(strtolower($unit), array(1 => 'kb','mb','gb','tb'))));
}
Wanting something similar to this and not quite liking the other solutions posted here for various reasons, I decided to write my own function:
function ConvertUserStrToBytes($str)
{
$str = trim($str);
$num = (double)$str;
if (strtoupper(substr($str, -1)) == "B") $str = substr($str, 0, -1);
switch (strtoupper(substr($str, -1)))
{
case "P": $num *= 1024;
case "T": $num *= 1024;
case "G": $num *= 1024;
case "M": $num *= 1024;
case "K": $num *= 1024;
}
return $num;
}
It adapts a few of the ideas presented here by Al Jey (whitespace handling) and John V (switch-case) but without the regex, doesn't call pow(), lets switch-case do its thing when there aren't breaks, and can handle some weird user inputs (e.g. " 123 wonderful KB " results in 125952). I'm sure there is a more optimal solution that involves fewer instructions but the code would be less clean/readable.
<?php
function byteconvert($input)
{
preg_match('/(\d+)(\w+)/', $input, $matches);
$type = strtolower($matches[2]);
switch ($type) {
case "b":
$output = $matches[1];
break;
case "kb":
$output = $matches[1]*1024;
break;
case "mb":
$output = $matches[1]*1024*1024;
break;
case "gb":
$output = $matches[1]*1024*1024*1024;
break;
case "tb":
$output = $matches[1]*1024*1024*1024;
break;
}
return $output;
}
$foo = "10mb";
echo "$foo = ".byteconvert($foo)." byte";
?>
Based on https://stackoverflow.com/a/17364338/1041470
Improvements:
Fixed bug with bytes suffix length,
Allowed to use double (float) values but only integers,
Reverted array which holds units,
Renamed variables,
Added comments.
/**
* Converts human readable file size into bytes.
*
* Note: This is 1024 based version which assumes that a 1 KB has 1024 bytes.
* Based on https://stackoverflow.com/a/17364338/1041470
*
* #param string $from
* Required. Human readable size (file, memory or traffic).
* For example: '5Gb', '533Mb' and etc.
* Allowed integer and float values. Eg., 10.64GB.
*
* #return int
* Returns given size in bytes.
*/
function cm_common_convert_to_bytes(string $from): ?int {
static $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$from = trim($from);
// Get suffix.
$suffix = strtoupper(trim(substr($from, -2)));
// Check one char suffix 'B'.
if (intval($suffix) !== 0) {
$suffix = 'B';
}
if (!in_array($suffix, $units)) {
return FALSE;
}
$number = trim(substr($from, 0, strlen($from) - strlen($suffix)));
if (!is_numeric($number)) {
// Allow only float and integer. Strings produces '0' which is not corect.
return FALSE;
}
return (int) ($number * pow(1024, array_flip($units)[$suffix]));
}
I was just looking for this function and took on the challenge to try to improve in it and got it to TWO lines :) Uses a similar regex to Eugene's to validate/extract values, but avoids the switch statement. Can accept long '10MB','10mb' and short '10M','10m' values, decimal values and always returns an integer. Invalid strings return 0
function to_bytes( $str )
{
if( ! preg_match('/^([\d.]+)([BKMGTPE]?)(B)?$/i', trim($str), $m) ) return 0;
return (int) floor($m[1] * ( $m[2] ? (1024**strpos('BKMGTPE', strtoupper($m[2]))) : 1 ));
}
One more solution (IEC):
<?php
class Filesize
{
const UNIT_PREFIXES_POWERS = [
'B' => 0,
'' => 0,
'K' => 1,
'k' => 1,
'M' => 2,
'G' => 3,
'T' => 4,
'P' => 5,
'E' => 6,
'Z' => 7,
'Y' => 8,
];
public static function humanize($size, int $precision = 2, bool $useBinaryPrefix = false)
{
$base = $useBinaryPrefix ? 1024 : 1000;
$limit = array_values(self::UNIT_PREFIXES_POWERS)[count(self::UNIT_PREFIXES_POWERS) - 1];
$power = ($_ = floor(log($size, $base))) > $limit ? $limit : $_;
$prefix = array_flip(self::UNIT_PREFIXES_POWERS)[$power];
$multiple = ($useBinaryPrefix ? strtoupper($prefix) . 'iB' : $prefix . 'B');
return round($size / pow($base, $power), $precision) . $multiple;
}
// ...
}
Source:
https://github.com/mingalevme/utils/blob/master/src/Filesize.php
https://github.com/mingalevme/utils/blob/master/tests/FilesizeTest.php
I know this is a relative old topic, but here's a function that I sometimes have to use when I need this kind of stuff; You may excuse for if the functions dont work, I wrote this for hand in a mobile:
function intobytes($bytes, $stamp = 'b') {
$indx = array_search($stamp, array('b', 'kb', 'mb', 'gb', 'tb', 'pb', 'yb'));
if ($indx > 0) {
return $bytes * pow(1024, $indx);
}
return $bytes;
}
and as compact
function intobytes($bytes, $stamp='b') {$indx=array_search($stamp,array('b','kb','mb','gb','tb','pb','yb'));if($indx > 0){return $bytes * pow(1024,$indx);} return $bytes;}
Take care!
Brodde85 ;)
Here is a little more cleaned version according to the standards (Using answer above):
/**
* Format kb, mb, gb, tb to bytes
*
* #param integer $size
* #return integer
*/
function formatToBytes ($size)
{
$aUnits = array('bytes' => 0, 'KB' => 1, 'MB' => 2, 'GB' => 3, 'TB' => 4);
$sUnit = strtoupper(trim(substr($size, -2)));
if (intval($sUnit) !== 0) {
$sUnit = 'bytes';
}
if (!in_array($sUnit, array_keys($aUnits))) {
return false;
}
$iUnits = trim(substr($size, 0, strlen($size) - 2));
if (!intval($iUnits) == $iUnits) {
return false;
}
return $iUnits * pow(1024, $aUnits[$sUnit]);
}
Let's say i check if
$strig = "red-hot-chili-peppers-californication";
already exists in my database:
$query = dbquery("SELECT * FROM `videos` WHERE `slug` = '".$strig."';");
$checkvideo = dbrows($query);
if($checkvideo == 1){
// the code to be executed to rename $strig
// to "red-hot-chili-peppers-californication-2"
// it would be great to work even if $string is defined as
// "red-hot-chili-peppers-californication-2" and
// rename $string to "red-hot-chili-peppers-californication-3" and so on...
}
I want to do this to create unique slugs for a more friendly url structure.
Thanks.
I can offer you the source of Codeigniter's increment_string() function:
/**
* CodeIgniter String Helpers
*
* #package CodeIgniter
* #subpackage Helpers
* #category Helpers
* #author ExpressionEngine Dev Team
* #link http://codeigniter.com/user_guide/helpers/string_helper.html
*/
/**
* Add's _1 to a string or increment the ending number to allow _2, _3, etc
*
* #param string $str required
* #param string $separator What should the duplicate number be appended with
* #param string $first Which number should be used for the first dupe increment
* #return string
*/
function increment_string($str, $separator = '_', $first = 1)
{
preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
}
Increments a string by appending a number to it or increasing the
number. Useful for creating "copies" or a file or duplicating database
content which has unique titles or slugs.
Usage example:
echo increment_string('file', '_'); // "file_1"
echo increment_string('file', '-', 2); // "file-2"
echo increment_string('file-4'); // "file-5"
$str = "some-string-that-might-end-in-a-number";
$strLen = strlen($str);
//check the last character of the string for number
if(intval($str[$strLen-1])>0)
{
//Now we replace the last number with the number+1
$newNumber = intval($str[$strLen-1]) +1;
$str = substr($str, 0, -1).$newNumber;
}
else
{
//Now we append a number to the end;
$str .= "-1";
}
Of course the limitation of this is that it can only get the last digit.. what if the number was 10?
$str = "some-string-that-might-end-in-a-number";
$strLen = strlen($str);
$numberOfDigits = 0;
for($i=$strLen-1; $i>0; $i--)
{
if(intval($str[$i])==0)
{
$numberOfDigits = $strLen-($i-1);
break;
}
}
//Now lets do the digit modification
$newNumber = 0;
for($i=1; $i<=$numberOfDigits; $i++)
{
$newNumber += intval($str[$strLen-$i])*((10*$i)-10));
}
if($newNumber == 0)
{ $newNumber = 1; }
$newStr = "-{$newNumber}";
//Now lets add the new string to the old one
$str = substr($str, 0, ($numberOfDigits*-1)).$newNumber;
The function levenshtein in PHP works on strings with maximum length 255. What are good alternatives to compute a similarity score of sentences in PHP.
Basically I have a database of sentences, and I want to find approximate duplicates.
similar_text function is not giving me expected results. What is the easiest way for me to detect similar sentences like below:
$ss="Jack is a very nice boy, isn't he?";
$pp="jack is a very nice boy is he";
$ss=strtolower($ss); // convert to lower case as we dont care about case
$pp=strtolower($pp);
$score=similar_text($ss, $pp);
echo "$score %\n"; // Outputs just 29 %
$score=levenshtein ( $ss, $pp );
echo "$score\n"; // Outputs '5', which indicates they are very similar. But, it does not work for more than 255 chars :(
The levenshtein algorithm has a time complexity of O(n*m), where n and m are the lengths of the two input strings. This is pretty expensive and computing such a distance for long strings will take a long time.
For whole sentences, you might want to use a diff algorithm instead, see for example: Highlight the difference between two strings in PHP
Having said this, PHP also provides the similar_text function which has an even worse complexity (O(max(n,m)**3)) but seems to work on longer strings.
I've found the Smith Waterman Gotoh to be the best algorithm for comparing sentences. More info in this answer. Here is the PHP code example:
class SmithWatermanGotoh
{
private $gapValue;
private $substitution;
/**
* Constructs a new Smith Waterman metric.
*
* #param gapValue
* a non-positive gap penalty
* #param substitution
* a substitution function
*/
public function __construct($gapValue=-0.5,
$substitution=null)
{
if($gapValue > 0.0) throw new Exception("gapValue must be <= 0");
//if(empty($substitution)) throw new Exception("substitution is required");
if (empty($substitution)) $this->substitution = new SmithWatermanMatchMismatch(1.0, -2.0);
else $this->substitution = $substitution;
$this->gapValue = $gapValue;
}
public function compare($a, $b)
{
if (empty($a) && empty($b)) {
return 1.0;
}
if (empty($a) || empty($b)) {
return 0.0;
}
$maxDistance = min(mb_strlen($a), mb_strlen($b))
* max($this->substitution->max(), $this->gapValue);
return $this->smithWatermanGotoh($a, $b) / $maxDistance;
}
private function smithWatermanGotoh($s, $t)
{
$v0 = [];
$v1 = [];
$t_len = mb_strlen($t);
$max = $v0[0] = max(0, $this->gapValue, $this->substitution->compare($s, 0, $t, 0));
for ($j = 1; $j < $t_len; $j++) {
$v0[$j] = max(0, $v0[$j - 1] + $this->gapValue,
$this->substitution->compare($s, 0, $t, $j));
$max = max($max, $v0[$j]);
}
// Find max
for ($i = 1; $i < mb_strlen($s); $i++) {
$v1[0] = max(0, $v0[0] + $this->gapValue, $this->substitution->compare($s, $i, $t, 0));
$max = max($max, $v1[0]);
for ($j = 1; $j < $t_len; $j++) {
$v1[$j] = max(0, $v0[$j] + $this->gapValue, $v1[$j - 1] + $this->gapValue,
$v0[$j - 1] + $this->substitution->compare($s, $i, $t, $j));
$max = max($max, $v1[$j]);
}
for ($j = 0; $j < $t_len; $j++) {
$v0[$j] = $v1[$j];
}
}
return $max;
}
}
class SmithWatermanMatchMismatch
{
private $matchValue;
private $mismatchValue;
/**
* Constructs a new match-mismatch substitution function. When two
* characters are equal a score of <code>matchValue</code> is assigned. In
* case of a mismatch a score of <code>mismatchValue</code>. The
* <code>matchValue</code> must be strictly greater then
* <code>mismatchValue</code>
*
* #param matchValue
* value when characters are equal
* #param mismatchValue
* value when characters are not equal
*/
public function __construct($matchValue, $mismatchValue) {
if($matchValue <= $mismatchValue) throw new Exception("matchValue must be > matchValue");
$this->matchValue = $matchValue;
$this->mismatchValue = $mismatchValue;
}
public function compare($a, $aIndex, $b, $bIndex) {
return ($a[$aIndex] === $b[$bIndex] ? $this->matchValue
: $this->mismatchValue);
}
public function max() {
return $this->matchValue;
}
public function min() {
return $this->mismatchValue;
}
}
$str1 = "Jack is a very nice boy, isn't he?";
$str2 = "jack is a very nice boy is he";
$o = new SmithWatermanGotoh();
echo $o->compare($str1, $str2);
You could try using similar_text.
It can get quite slow with 20,000+ characters (3-5 seconds) but your example you mention using only sentences, this will work just fine for that usage.
One thing to note is when comparing string of different sizes you will not get 100%. For example if you compare "he" with "head" you would only get a 50% match.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
The community reviewed whether to reopen this question 10 months ago and left it closed:
Opinion-based Update the question so it can be answered with facts and citations by editing this post.
Improve this question
Is there any way to create unique keys like those used in YouTube video urls (ex: https://www.youtube.com/watch?v=nWChTnkVdKE)?
The idea is to convert a unique integer (such as current time) in an other mathematical base.
A very simple way in PHP :
// With this precision (microsecond) ID will looks like '2di2adajgq6h'
// From PHP 7.4.0 this is needed, otherwise a warning is displayed
$cleanNumber = preg_replace( '/[^0-9]/', '', microtime(false) );
$id = base_convert($cleanNumber, 10, 36);
// With less precision (second) ID will looks like 'niu7pj'
$id = base_convert(time(), 10, 36);
A small PHP class to generate YouTube-like hashes from one or many numbers. Use hashids when you do not want to expose your database ids to the user.
Source: https://github.com/ivanakimov/hashids.php
Use whichever you like :-)
// Generates Alphanumeric Output
function generateRandomID() {
// http://mohnish.in
$required_length = 11;
$limit_one = rand();
$limit_two = rand();
$randomID = substr(uniqid(sha1(crypt(md5(rand(min($limit_one, $limit_two), max($limit_one, $limit_two)))))), 0, $required_length);
return $randomID;
}
// Generates only alphabetic output
function anotherRandomIDGenerator() {
// Copyright: http://snippets.dzone.com/posts/show/3123
$len = 8;
$base='ABCDEFGHKLMNOPQRSTWXYZabcdefghjkmnpqrstwxyz';
$max=strlen($base)-1;
$activatecode='';
mt_srand((double)microtime()*1000000);
while (strlen($activatecode)<$len+1)
$activatecode.=$base{mt_rand(0,$max)};
return $activatecode;
}
For me best algorithm is this: Create Youtube-Like IDs With PHP/Python/Javascript/Java/SQL
<?php
/**
* Translates a number to a short alhanumeric version
*
* Translated any number up to 9007199254740992
* to a shorter version in letters e.g.:
* 9007199254740989 --> PpQXn7COf
*
* specifiying the second argument true, it will
* translate back e.g.:
* PpQXn7COf --> 9007199254740989
*
* this function is based on any2dec && dec2any by
* fragmer[at]mail[dot]ru
* see: http://nl3.php.net/manual/en/function.base-convert.php#52450
*
* If you want the alphaID to be at least 3 letter long, use the
* $pad_up = 3 argument
*
* In most cases this is better than totally random ID generators
* because this can easily avoid duplicate ID's.
* For example if you correlate the alpha ID to an auto incrementing ID
* in your database, you're done.
*
* The reverse is done because it makes it slightly more cryptic,
* but it also makes it easier to spread lots of IDs in different
* directories on your filesystem. Example:
* $part1 = substr($alpha_id,0,1);
* $part2 = substr($alpha_id,1,1);
* $part3 = substr($alpha_id,2,strlen($alpha_id));
* $destindir = "/".$part1."/".$part2."/".$part3;
* // by reversing, directories are more evenly spread out. The
* // first 26 directories already occupy 26 main levels
*
* more info on limitation:
* - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/165372
*
* if you really need this for bigger numbers you probably have to look
* at things like: http://theserverpages.com/php/manual/en/ref.bc.php
* or: http://theserverpages.com/php/manual/en/ref.gmp.php
* but I haven't really dugg into this. If you have more info on those
* matters feel free to leave a comment.
*
* The following code block can be utilized by PEAR's Testing_DocTest
* <code>
* // Input //
* $number_in = 2188847690240;
* $alpha_in = "SpQXn7Cb";
*
* // Execute //
* $alpha_out = alphaID($number_in, false, 8);
* $number_out = alphaID($alpha_in, true, 8);
*
* if ($number_in != $number_out) {
* echo "Conversion failure, ".$alpha_in." returns ".$number_out." instead of the ";
* echo "desired: ".$number_in."\n";
* }
* if ($alpha_in != $alpha_out) {
* echo "Conversion failure, ".$number_in." returns ".$alpha_out." instead of the ";
* echo "desired: ".$alpha_in."\n";
* }
*
* // Show //
* echo $number_out." => ".$alpha_out."\n";
* echo $alpha_in." => ".$number_out."\n";
* echo alphaID(238328, false)." => ".alphaID(alphaID(238328, false), true)."\n";
*
* // expects:
* // 2188847690240 => SpQXn7Cb
* // SpQXn7Cb => 2188847690240
* // aaab => 238328
*
* </code>
*
* #author Kevin van Zonneveld <kevin#vanzonneveld.net>
* #author Simon Franz
* #author Deadfish
* #author SK83RJOSH
* #copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net)
* #license http://www.opensource.org/licenses/bsd-license.php New BSD Licence
* #version SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $
* #link http://kevin.vanzonneveld.net/
*
* #param mixed $in String or long input to translate
* #param boolean $to_num Reverses translation when true
* #param mixed $pad_up Number or boolean padds the result up to a specified length
* #param string $pass_key Supplying a password makes it harder to calculate the original ID
*
* #return mixed string or long
*/
function alphaID($in, $to_num = false, $pad_up = false, $pass_key = null)
{
$out = '';
$index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$base = strlen($index);
if ($pass_key !== null) {
// Although this function's purpose is to just make the
// ID short - and not so much secure,
// with this patch by Simon Franz (http://blog.snaky.org/)
// you can optionally supply a password to make it harder
// to calculate the corresponding numeric ID
for ($n = 0; $n < strlen($index); $n++) {
$i[] = substr($index, $n, 1);
}
$pass_hash = hash('sha256',$pass_key);
$pass_hash = (strlen($pass_hash) < strlen($index) ? hash('sha512', $pass_key) : $pass_hash);
for ($n = 0; $n < strlen($index); $n++) {
$p[] = substr($pass_hash, $n, 1);
}
array_multisort($p, SORT_DESC, $i);
$index = implode($i);
}
if ($to_num) {
// Digital number <<-- alphabet letter code
$len = strlen($in) - 1;
for ($t = $len; $t >= 0; $t--) {
$bcp = bcpow($base, $len - $t);
$out = $out + strpos($index, substr($in, $t, 1)) * $bcp;
}
if (is_numeric($pad_up)) {
$pad_up--;
if ($pad_up > 0) {
$out -= pow($base, $pad_up);
}
}
} else {
// Digital number -->> alphabet letter code
if (is_numeric($pad_up)) {
$pad_up--;
if ($pad_up > 0) {
$in += pow($base, $pad_up);
}
}
for ($t = ($in != 0 ? floor(log($in, $base)) : 0); $t >= 0; $t--) {
$bcp = bcpow($base, $t);
$a = floor($in / $bcp) % $base;
$out = $out . substr($index, $a, 1);
$in = $in - ($a * $bcp);
}
}
return $out;
}
Here is a small function that generates unique key randomly each time. It has very fewer chances to repeat same unique ID.
function uniqueKey($limit = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randstring = '';
for ($i = 0; $i < $limit; $i++) {
$randstring .= $characters[rand(0, strlen($characters))];
}
return $randstring;
}
I was looking for a very quick and simple solution (that was NOT intended to be secure in any way) that would replicate the actual hash code upon regeneration (unlike PHP's microtime unique id). This one did the trick.
http://php.net/manual/en/book.hash.php
So:
for($i=0;$i<count($myArray);$i++) {
$hashCode = "#".hash('crc32b', $myArray[$i]."-".$i);
}
Yields:
#179507fa
#8e9c5640
#f99b66d6
#67fff375
#10f8c3e3
etc
I won't pretend to know how all the different hash algorithms are used but for non-secure uses (like link #anchors) this is handy.
From comments on here:
<?php
function generateRandStr($length){
$randstr = "";
for($i=0; $i<$length; $i++){
$randnum = mt_rand(0,61);
if($randnum < 10){
$randstr .= chr($randnum+48);
}else if($randnum < 36){
$randstr .= chr($randnum+55);
}else{
$randstr .= chr($randnum+61);
}
}
return $randstr;
}
?>
Simply use:
generateRandStr(10);
Sample output: $%29zon(4f
You can mess around with this function to generate just alphanumeric, or just alphabetic characters.
For most purposes uniqid will suffice, if you need to make sure there's absolutely no clash, more convoluted measures are necessary.
all the methods above are good but make sure you know and not assume the generated string is unique. What I have done in the past is made a recursive function that checks the string against my database and if it is unique it returns the value, otherwise it just runs again. This will hardly happen though depending how long your unique string is. Its just good to know its unique.
<?php
function keygen(){
$chars = "bcdfghjklmnpqrstvwxyz";
$chars .= "BCDFGHJKLMNPQRSTVWXYZ";
$chars .= "0123456789";
while(1){
$key = '';
srand((double)microtime()*1000000);
for($i = 0; $i < 10; $i++){
$key .= substr($chars,(rand()%(strlen($chars))), 1);
}
break;
}
return $key;
}
//echo keygen();
?>