I need to find the number of iterations need to reach the given string using character incrementing in php. I have code the below approach but it is a resource and time-consuming.
$x = 'a';
$num = 1;
while ($x != 'zzzz'){
$x++;
$num++;
echo "$num: $x <br>";
}
above code starts lookup from charactera
and goes through all the way tozzzz
, is somehow good for small string but it blasts the memory for long strings, is there any shorter way to calculate this?
A simplified answer which reduces the amount of loops to 1 (comments in code)....
// String we want to find the value from
$end = 'ae';
// The list of all possible characters in the alphabet
$alphabet = range('a', 'e');
// Flip this array so that the letter becomes the key and the value is index (0 based)
$alphabetSearch = array_flip($alphabet);
$result = 0;
$endLen = strlen($end);
// Use the target string as an array an iterate over each char
for ($i = 0; $i < $endLen; $i++) {
// Multiply the result by the number of chars in the alphaber to maintain the scale of each char
$result *= count($alphabet);
// Add on the index of the new char (+1 as it's 0 based)
$result += ($alphabetSearch[$end[$i]] ?? 0) + 1;
}
echo $result;
Which in this example (ae with a,b,c,d,e as the alphabet) gives
10
I have implemented this using PHP, the full description is included below but the basic idea is
code.
{-code-1}
Hi Guys, it might worth to mention it because this operation is needed in many programs and codes such as cryptography, brute force attacks, web scrapping, and many other operations, here is the idea behind the code.
using methodcharacterLookupOnAlphabetByString
we are finding the location of the alphabet character on the alphabet table for exampleA
is the number1
st character on the alphabet tableZ
is26
th character on the alphabet table, the method return results in this form.
[
{
"x": 24
},
{
"b": 2
},
{
"c": 3
}
]
I have given the stringxbc
to thecharacterLookupOnAlphabetByString
method and it returned the above array. until now we just find the position of each character of string in the alphabet table. let's go to the main operation
next, we are going to find the total iteration for the string characters. usingfindCharacterLevelFromString
method. this method will receive a string and then pass it to thecharacterLookupOnAlphabetByString
method and receive back the above array-like result, next will need to find the number of characters in the given array.
next, we doforeach
loop on the returned array, next, since the index of number for every character is that character we do not know the character which is entered in the string we do another innerforeach
loop for each character(please check the array structure), inside the inner loop we find the power of26
(which is total number of alphabet character) on (total character - 1) then the result is going to multiply to the position of the character in alphabet table
Just get the excel column index of each string and subtract them to get the distance.
For every new position in the string, the previous results will be multiplied by 26 and we will add the ordinal of the current character to get it's exact state value.
Snippet:
<?php
function getExcelColumnIndex($str){
$columnIndex = 0;
$len = strlen($str);
for($i = 0; $i < $len; ++$i){
$columnIndex *= 26;
$columnIndex += ord($str[$i]) - ord('a') + 1;
}
return $columnIndex;
}
echo getExcelColumnIndex("zzzz") - getExcelColumnIndex("a");
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.