I have dilemma. I need unique string in my database table to show it in url (I don't show id in url).
I want generate in php unique string check it is unique in table if not generate new.
What do you think in terms of performance for a string with a length of 8 characters, and with a large number of rows (about 100,000 rows).
You can create a unique id in php using uniqid().
When generating a new ID, check the DB to see if it exists. If it does, create a new one. Assuming you set up an index on that column in the database, these checks will not be very expensive.
In pseudo(-ish) code :
while (true) {
$id = uniqid();
if (!id_exists_in_db($id)) // we have a unique id
break;
}
Instead of generating a unique key besides the id, you can also create a function that hashes or encodes the id to a string. For instance, you can convert the integer to an 8 character hexidecimal representation. You can make it a little more complex if you like, but still, it's fairly easy to write a function that uses a fixed formula to convert a number to 8 letters and vice versa. That way, you don't need to store this extra data, and you don't have to worry about the value being unique.
<?php
function translate($key, $back)
{
$key = str_pad($key, 8, '0', STR_PAD_LEFT);
$a = array('1','2','3','4','5','6','7','8','9','0');
$b = array('a','b','c','d','e','f','g','h','i','j');
if ($back) {$c = $b; $b = $a; $a = $c;}
return str_replace($a, $b, $key);
}
function encode_id($int)
{
return translate($int, false);
}
function decode_id($key)
{
return (int)translate($key, true);
}
$key = encode_id(40000);
$int = decode_id($key);
echo $key;
echo '<br>';
echo $int;
Related
I would like to generate a random unique alpha string in PHP or Laravel, that would it be only 5 characters long, the problem is that in all the examples I've found, they mention how to generate alpha-numeric strings, and I need only alpha characters, and also it to be upper case, how can I do it? thank you.
Str::random(5) can be used to generate non-unique alpha-numeric strings, but if you want unique, alpha only, then you'll need some kind of helper function.
Laravel's Collection class has a number of methods that makes this rather trivial:
public function randomString() {
$alphaString = collect(array_merge(range('a', 'z'), range('A', 'Z')))
->shuffle()
->take(5)
->implode('');
}
This will generate 5 character alpha strings (without repeating values, that can be done too if you need more combinations), but still doesn't handle the "unique" part. To handle that, you simply need to check the generated value, and regenerate it if it already exists somewhere. A common use-case is using this value as a unique column for a Model:
public function randomString() {
$alphaString = collect(array_merge(range('a', 'z'), range('A', 'Z')))
->shuffle()
->take(5)
->implode('');
if (Model::where('column', $alphaString)->exists()) {
return $this->randomString();
}
return $alphaString;
}
If you want to allow repeated values, then you'd need to loop and shuffle:
public function randomString() {
$values = collect(array_merge(range('a', 'z'), range('A', 'Z')));
$characters = collect();
while ($characters->count() < 5) {
$characters->push($values->shuffle()->first());
}
$alphaString = $characters->implode('');
if (Model::where('column', $alphaString)->exists()) {
return $this->randomString();
}
return $alphaString;
}
This allows for more combinations, but you're still rather limited with only 5 characters.
Edit: If you're not using Laravel, native array methods work too, as the Collection class is basically just a fancy array anyway:
$values = array_merge(range('a', 'z'), range('A', 'Z'));
$characters = [];
while (count($characters) < 5) {
shuffle($values);
$characters[] = $values[0];
}
$alphaString = implode($characters);
And, bonus, an interactive version to verify:
https://3v4l.org/tR5IF#v8.1.13
The following is the code
<?php
$id ="202883-202882-202884-0";
$str = implode('-',array_unique(explode('-', $id)));
echo $str;
?>
The result is
202883-202882-202884-0
for $id ="202883-202882-202882-0";, result is 202883-202882-0
I would like to replace the duplicate value with zero, so that the result should be like 202883-202882-0-0, not just remove it.
and for $id ="202883-0-0-0";, result should be 202883-0-0-0. zero should not be replaced, repeating zeros are allowed.
How can I archive that?
More info:
I want to replace every duplicate numbers. Because this is for a product comparison website. There will be only maximum 4 numbers. each will be either a 6 digit number or single digit zero. all zero means no product was selected. one 6 digit number and 3 zero means, one product selected and 3 blank.
Each 6 digit number will collect data from database, I dont want to allow users to enter same number multiple times (will happen only if the number is add with the URL manually.).
Update: I understand that my question was not clear, may be my English is poor.
Here is more explanation, this function is for a smartphone comparison website.
The URL format is sitename.com/compare.html?id=202883-202882-202889-202888.
All three numbers are different smartphones(their database product ID).
I dont want to let users to type in the same product ID like id=202883-202882-202882-202888. It will not display two 202882 results in the website, but it will cause some small issues. The URL will be same without change, but the internal PHP code should consider it as id=202883-202882-202888-0.
The duplicates should be replaced as zero and added to the end.
There will be only 4 numbers separated by "-".
The following examples might clear the cloud!
if pid=202883-202882-202889-202888 the result should be 202883-202882-202889-202888
if pid=202883-202883-202883-202888 the result should be 202888-0-0-0
if pid=202883-202882-202883-202888 the result should be 202883-202882-202888-0
if pid=202882-202882-202882-202882 the result should be 202882-0-0-0
I want to allow only either 6 digit numbers or single digit zero through the string.
if pid=rgfsdg-fgsdfr4354-202883-0 the result should be 202883-0-0-0
if pid=fasdfasd-asdfads-adsfds-dasfad the result should be 0-0-0-0
if pid=4354-45882-445202882-202882 the result should be 202882-0-0-0
It is too complicated for me create, I know there are bright minds out there who can do it much more efficiently than I can.
You can do a array_unique (preserves key), then fill the gaps with 0. Sort by key and you are done :)
+ on arrays will unify the arrays but prioritizes the one on the left.
Code
$input = "0-1-1-3-1-1-3-5-0";
$array = explode('-', $input);
$result = array_unique($array) + array_fill(0, count($array), 0);
ksort($result);
var_dump(implode('-',$result));
Code (v2 - suggested by mickmackusa) - shorter and easier to understand
Fill an array of the size of the input array. And replace by leftover values from array_unique. No ksort needed. 0s will be replaced at the preserved keys of array_unique.
$input = "0-1-1-3-1-1-3-5-0";
$array = explode('-', $input);
$result = array_replace(array_fill(0, count($array), 0), array_unique($array));
var_export($result);
Working example.
Output
string(17) "0-1-0-3-0-0-0-5-0"
Working example.
references
ksort - sort by key
array_fill - generate an array filled with 0 of a certain length
This is another way to do it.
$id = "202883-202882-202882-0-234567-2-2-45435";
From the String you explode the string into an array based on the delimiter which in this case is '-'/
$id_array = explode('-', $id);
Then we can loop through the array and for every unique entry we find, we can store it in another array. Thus we are building an array as we search through the array.
$id_array_temp = [];
// Loop through the array
foreach ($id_array as $value) {
if ( in_array($value, $id_array_temp)) {
// If the entry exists, replace it with a 0
$id_array_temp[] = 0;
} else {
// If the entry does not exist, save the value so we can inspect it on the next loop.
$id_array_temp[] = $value;
}
}
At the end of this operation we will have an array of unique values with any duplicates replaced with a 0.
To recreate the string, we can use implode...
$str = implode('-', $id_array_temp);
echo $str;
Refactoring this, using a ternary to replace the If,else...
$id_array = explode('-', $id);
$id_array_temp = [];
foreach ($id_array as $value) {
$id_array_temp[] = in_array($value, $id_array_temp) ? 0 : $value;
}
$str = implode('-', $id_array_temp);
echo $str;
Output is
202883-202882-0-0-234567-2-0-45435
This appears to be a classic XY Problem.
The essential actions only need to be:
Separate the substrings in the hyphen delimited string.
Validate that the characters in each substring are in the correct format AND are unique to the set.
Only take meaningful action on qualifying value.
You see, there is no benefit to replacing/sanitizing anything when you only really need to validate the input data. Adding zeros to your input just creates more work later.
In short, you should use a direct approach similar to this flow:
if (!empty($_GET['id'])) {
$ids = array_unique(explode('-', $_GET['id']));
foreach ($ids as $id) {
if (ctype_digit($id) && strlen($id) === 6) {
// or: if (preg_match('~^\d{6}$~', $id)) {
takeYourNecessaryAction($id);
}
}
}
At the moment my "method" checks if the array_key is in the array, i need to add that if the key length is 13 it is okay, but when it is longer it has to delete the like first 5 numbers of the list. My code searches the price according to my key out of the database, and compares it to a list where the "keys" are a bit different. So to make it easier to understand:
The key in my database is: 2840529503100
The key in the table where i have to get the price from is: 000002840529503100
So what i basically need to do is get a substring from the "list" or "table" which takes the last 13 numbers because the right keys of my database are always 13 numbers long.
$teile = array();
while ($dbRow = $sqlres->fetch_assoc()) {
$teile[$dbRow['nummer']] = $dbRow;
if (array_key_exists($fileRow[0], $teile)) {
If your first five 0 values is constant means you can use substr() like this:
$str = '000002840529503100';
echo $str2 = substr($str, 5);
Another method for substr() is only keep the last last 13 digits like this:
$str = '000002840529503100';
echo $str2 = substr($str, -13);
Else to remove the first 0 values you can use ltrim() like this:
echo ltrim('000002840529503100', '0');
Note: without quotes these two functions won't work.
As an alternative to Nawins solution you can also use PHP Typecasting to automatically remove the leading zeros.
NOTE: for my example; I can't see which variable you're trying to change. But assumed the first...
Thus:
$fileRow[0] = "000002840529503100"
(int)$fileRow[0] = "2840529503100";
This would make:
if (array_key_exists((int)$fileRow[0], $teile)) {
...
}
BEWARE:
This example doesn't change the stored value of $fileRow[0] so using it as a future key reference (~print $array[$fileRow[0]]) will still use the longer number until the (int) version has been set as the variable/key value, example:
$fileRow[0] = (int)$fileRow[0];
So I have a small issue over PHP. I think I've messed the foreach loops a bit. Here's the code:
<?php
error_reporting(0);
class Hash{
public function hashing($password,$hashsum)
{
$iter = date('sh');
$poyl = array($iter,strrev($iter));
foreach ($poyl as $key) {
$itg = array('1','2','3','4','5'); # if == any array element
foreach($itg as $v){
if ($key == $v){ # has to be checked here
$algo = 'TIGER128,3';
} elseif ($key == rand(31,60)){
$algo = 'HAVAL128,4';
} # else
}
} # foreach
return hash($algo,$password.strtok($password,'7'));
}
}
$obj = new Hash();
print $obj->hashing('text',hash($algo,$password.strtok($password,'7')));
What I aim to achieve here is the following: this is supposed to be a class that would calculate semi-polymorphic hash values. In order for this kinda false polymorphism to work, I need to at least have two different hashing algorithms. (establishing dependency and correlation with a database would be considered afterwards).
So my problem is that I need to perform a check that would determine whether the first character (integer in this case) is equal to any of the elements in the $itg array (or if it's equal to any number ranging 1-30). If so, the algorithm will be TIGER128,3; if not (and first character of the date variable is equal to any value ranging 31-60) then algorithm applied should be HAVAL128,4.
The code is totally messed up. I'm using the date function to generate integers and compare them afterwards because I couldn't think of anything that fast (means this will defo be changed).
if you just want to compare what is in that array you could use in_array()
$array = array(1,2,3,4);
$str = '1abcdef';
in_array($str[0], $array) // Returns TRUE;
http://uk1.php.net/in_array
I have this piece of code:
$result = new stdClass();
foreach ($array as $index => $value) {
if(is_numeric($value)){
$int = (int)$value;
$double = (double)$value;
if($int == $double)
$value = $int;
else
$value = $double;
}
$index = strtolower($index);
$result->$index = $value;
}
And it worked for ages. Now I got a problem with it. I have a column in my database that has numbers (big numbers) in it. But they're not numbers, they're varchar and those numbers are not for mathematical purpose. Unfortunately, since the column is fully filled with numbers only, it passes the is_numeric test, but since it's a giant number, it loses data due to memory limitation (4 billions, I think).
Anyway, how can I check if after the cast I lost data or not to my variable?
Thanks.
if($value<=PHP_INT_MAX) ... // safe to convert
else // not safe
Convert it back and see if it gives the same value as the source.