2
Is there any way to turn whole words into unique numbers so that I can do the reverse process if necessary? Like it was a unique ID of that word, like
Palavra -> Numero -> Palavra
oi -> 09368 -> oi
2
Is there any way to turn whole words into unique numbers so that I can do the reverse process if necessary? Like it was a unique ID of that word, like
Palavra -> Numero -> Palavra
oi -> 09368 -> oi
5
I will keep this answer here because it can help someone, the question was better clarified in the comments and the problem was another. But the other cannot be solved without having criteria to define it, which can even be complicated if you need a conversion to a single number. Even if it is guaranteed that there can only be words (therefore short), the number of combinations to avoid repetition is so large that it is better to leave the text even.
I imagine what you want is the dictionary, or array associative, as this feature is best known in PHP:
$palavras = array(
"oi" => 09368,
"tchau" => 01234,
"palavra" => 34986,
"abobrinha" => 72494);
$codigo = $palavras["oi"]; //busca rápida
$palavra = array_search(01234, $palavras); //busca lenta
The first to be done through function hash, is very fast (complexity O(1)). The search for the value is slow (complexity O(n)). It is possible to obtain complexity O(log n) which is close to O(1) if the array is guaranteed to have the values sorted. Then it is possible to perform a binary search.
It is still possible, if memory is not an impediment, to have a second array with the values reversed. There it is possible to search in O(1) in both. Thus:
$palavras = array(
09368 => "oi",
01234 => "tchau",
34986 => "palavra",
72494 => "abobrinha");
I put in the Github for future reference.
I kept the zeroes meaningless, but obviously they’re not necessary
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
Give more details on how this should be done. Show what you have already done.
– Maniero
It would be something like How to turn numeric digits into numbers in full?, How to convert number to number in spellnumber in PHP? ?
– rray
@bigown did nothing at first, I wonder if there is something predefined for PHP or existing, if not, what comes to mind would be to create array with letters with predefined codes type A => 01, B = 02....
– Elaine
@rray is not what I seek.
– Elaine
@Elaine needs to establish the criteria, is there a direct relation? The numbers are calculated by the word? It’s just a code previously determined?
– Maniero
Huum you want to encrypt a word in number and then decrypt? maybe p use ASCII codes
– rray
@bigown there is no direct relation to the word, it is like a car plate that identifies that specific car.
– Elaine
@rray will be? I tried to use the functions
ord
andchr
but they only go with one character at a time, and I would need an integer representing the word so that I can decipher it as you said yourself.– Elaine
@Elaine these codes are numbers or are numeric texts?
– Maniero
@Elaine your comments are contradictory. Define what you really want, have direct relationship or have no?
– Maniero
@bigown is required to be numbers (int). Maybe I misunderstood, the word number should be able to indicate which word generated it. You understand?
– Elaine
@Elaine what is the criteria to get this code? I had already assembled the answer without having the direct relationship.
– Maniero
@bigown nothing specific, performing the function is enough, you have ideas of how to do?
– Elaine
@Elaine No. There are numerous ways to do this. You need to have a criterion. You need to have rules on how that number should be found. You have to have the constraints of what you can and cannot do. How to deal with collisions, just to give an example.
– Maniero
@bigown have no idea about these points, as I said "what comes to mind would be to create array with letters with predefined codes type A => 01, B = 02...."
– Elaine
I gave an answer based on this, but if you want to calculate, you’d have to have criteria.
– Maniero
@Elaine vc could specify in the question some restrictions regarding the numbers that will be generated from a word, for example: Can be negative number? There is a maximum or minimum number?
– gato
@Elaine I would do this with a mathematical calculus, so that from each word I would generate a number for it, calculating vowels and consonants, there are other forms, but this may be an option.
– gato