I have so many Unicode strings and want to store them in MySQL database. Also I want to add an extra field such that represents the character identity of the string. For example:
String key
------ -----------
this is 1st string 113547858
this is first string 113547865
I go to school 524872354
As you may have noticed above, the first 2 keys are so close to each other, representing strings similarity, whereas the 3rd one is so far from them.
I don't want to use PHP's similar_text or levenshtein as they need two strings to check similarity, but I want to store a value for each single string to store in DB in order to put an index on it for future use.
Simple summation of the character codes of all characters of the string can be a solution?
Update:
Summation of a hash value at the level of every word of the string can also be a solution
Related
I need to generate unique alphanumeric string by checking mysql database. The string can be A-Z and 0-9 combination. I have try floor() and rand() function but there is a possibility to repeat same string if vast number of user try once.
If you generate random number/string it's always possible to get the same string. However, if you generate 16-char (or more) length string it's unlikely that you will ever get the same string.
You can generate random string and then check if it exist in database. In that very rare situation you can generate new random string in while loop until you find one that is free. As I wrote before, even 16 characters length text with [A-Z0-9] is almost impossible to repeat. It's 36^16 possible combinations.
You can also pregenerate let's say 1000 strings, sort them by random order and put into database. After that you simply choose first unsued string from database.
you can use uniqid() function, which is generate string current time based. If you want upper case letters use like this:
strtoupper(uniqid());
I want to generate fix string for name which is associated for that name only.
For example:
my string: test
generated string: elephant
If I write test again then it must generate string elephant in any browser or in any machine
Could anyone please help me with this?
I don't think jQuery is the right answer, but you could use it for handling XHRs to get the transformed string from a PHP back-end.
It sounds like you want deterministic hashing of strings to dictionary words. Using a single word as your result is far more likely to result in a collision; use 3 or more. Give your PHP back-end access to a large dictionary: a simple, non-profane word list, which must not change, in a file or a database table with a 0-based index. Pass it the input string to hash using something like sha256. Take equal length slices of the hash string and with each slice, look up one word by converting the slice's hexadecimal to an integer, add the integer to the previous index if it's not the first, modulo (%) the integer by the length of the dictionary to get an index and add the word at that index to your result.
I'm generating a random 10 character string with php, and inserting it into a column in my DB. My issue is that I want this string to be unique(in the database). I've thought of many ways of doing this, but wondering which way is the most efficient.
My PHP looks like this(random string can only be 10 chars long):
//generates an almost unique(not quite) ticket number
$pretrimmedtask = md5(uniqid(mt_rand(),true));
$tasknum = substr($pretrimmedtask ,0,10);
I then take this "unique" value and insert it. But because of the trim of the string, this value is by no means unique. I'm wondering what is the best way of making sure this value could never be duplicated, while still being efficient.
(I understand that querying the db to look for this value in there is possible... but I would rather do it in a more elegant fashion)
You should update your table and make the relevant column be a UNIQUE KEY, than try to insert the generated string, if no rows where inserted, generate another key and try again.
ALTER TABLE table_name ADD UNIQUE KEY (column_name);
The code below will try to INSERT a new row into table1, if unable it will try again with a different random generated $key.
IE. the query will not succeed if col2 has a unique key constraint and the value of $key already exists in the column.
function generate_random_string () {
$charset = array_merge (
range ('a', 'z'), range ('A','Z'), range ('0','0')
);
shuffle ($charset);
return join ('', array_slice ($charset, 0, 9));
}
/* ....................................................... */
do {
$key = generate_random_string ();
} while (
!mysql_query ("INSERT INTO table1 (col1,col2) VALUES (123, '$key')")
);
You can of course use your own algorithm for generating random strings.
NOTE: Make sure that the query can potentially succeed so that you don't get caught in an endless loop.
Create unique mysql index that covers only that field and insert the value in a loop until success.
Like:
while (true) {
$random = generate it;
try to insert;
if (inserted without errors) break;
}
Does it has to be 10 character. With crypt() you can generate 13 character long hashes.
Here http://www.php.net/manual/en/function.hash.php you can check the length of different hashing methods. None of them unfortunatelly produces exactly 10 character long string. But you can generate 8 character long string and add two characters.
Another possible solution that I came up with is using current date of generating the string. Unixtimestamp is only numbers and to long but we can convert date into 10 char string in the following manner
Create two arrays, first with keys from 1 til 31 and assign one character for each key (26 letters plus 10 numbers will do the trick), the second array need to have keys from 0 til 99 and have values of two charater long string.
Now take the day, month, year (2 digits), hour, minute and seconds of the current time and replace the value with the value from the array, where day and month take from the first array and the rest from the second. Combine that and you have 10 character long unique string.
Had this issue by myself.
I use (insert) time() and insert/row id (+ special letters array) md5-ed all together as one string and hashed - for some cookie purposes lets say. So, that key is exposed.
Insert (or row) id cannot be duplicated, and merged with unix timestamp (10 digits)+random letters and md5-ed all together creates surely unique "second key" somewhat harder to break what is available via cookies. In this case is impossible to break it.
But it is a hash.
If 10 chars is essential - as I can't find reason to be - you may create function for creating keys like (99999999999999999999-primary key)+substr 10 with letters included also, but that depends on a level of exposure of that key.
However, substr is not an option, and primary key role is simply - essential.
As seen in RFC2289 (S/KEY), there is a list of words that must be used when converting the hexadecimal string into a readable format.
How would i go about doing so?
The RFC mentions:
The one-time password is therefore
converted to, and accepted as, a
sequence of six short (1 to 4 letter)
English words. Each word is chosen
from a dictionary of 2048 words; at 11
bits per word, all one-time passwords
may be encoded.
Read more:
http://www.faqs.org/rfcs/rfc1760.html#ixzz0fu7QvXfe
Does this mean converting a hex into decimal and then using that as an index for an array of words. The other thing it could be is using a text encoding e.g. 1111 might equal dog in UTF-8 encoding
thanks in advance for your help!
There's no need to convert to decimal. If your hex value is a string, just convert it to a number (for example, with Integer.valueOf(value, 16)). Then use that number to look up the word. If you can store the whole dictionary in memory, use the number as the index. If you can't store it in memory, use it to control how far into the dictionary file you look (if every item is on a separate line, read that many lines into the file). If you've got a database somewhere, use the number as the table's key and select by key.
Hi I have got a column in my database which is set to Int.
But my data always starts with a 0 so whenever I add a new record, it strips the 0 off and I don't want it to do that incase the first character has to be a 1 at some point.
How can I overcome this issue?
Is the best way to use VARCHAR any then validate using PHP?
Update
If I enter 02118272 it is saved to the database as 2118272.
The integer 7 is the same thing as the integer 000000000000000000000000007. They're both... ya know... seven. When stored in the database, it's actually stored as something like 000000000000000000000000007 but most MySQL clients won't bother to show all those zeros.
If it's important in your application to show these numbers using a certain number of digits, you can add back as many leading zeros as you want using the str_pad() function:
str_pad($your_string, 10, '0', STR_PAD_LEFT);
Here, 10 is the length you want the string to be, '0' is the character that will get added on in order to make it that length, and STR_PAD_LEFT says to add characters to the left-hand side of the string.
If, on the other hand, the number '007' is fundamentally different than the number '7', then you will have to use a VARCHAR() field to store it. Those are no longer integers; they're strings with very different meanings.
What you should be storing in your database is data. Formatting of that data is the responsibility of applications, not the database itself.
I would store it as an integer and, if you need that to 7 decimal places with leading zeros, the right place to do that is after extraction of the data to your application.
I think that you should use varchar type for that field. If you want to convert a variable to integer in php you can simply do this:
$var=preg_replace("/[^\d]/","",$var);
with this you delete all characters that aren't numbers and then you can put this value into the db preserving the initial 0.