我需要使用 php 中的字符遞增來找到達到給定字串所需的迭代次數。我有以下方法的代碼,但它是一種資源和耗時。
$x = 'a';
$num = 1;
while ($x != 'zzzz'){
$x ;
$num ;
echo "$num: $x <br>";
}
上面的代碼從字符開始查找a并一直到zzzz,對小字串有點好處,但它會破壞長字串的記憶體,有沒有更短的方法來計算這個?
uj5u.com熱心網友回復:
一個簡化的答案,將回圈數量減少到 1(代碼中的注釋)......
// 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;
在這個例子中(ae 與 a,b,c,d,e 作為字母表)給出
10
uj5u.com熱心網友回復:
我已經使用 PHP 實作了這個,完整的描述在下面,但基本的想法是
- 在字母表中找到字串中每個字符的位置。
- 將 26 乘以字符總數 (az) - 1。
- 將第 2 步的結果乘以字母表中字符的位置。
代碼。
/**
* find iteration level from a string using the incrementing approach.
*
* @param $string string
* @return number
*/
public function findCharacterLevelFromString(($string)){
$array = $this->characterLookupOnAlphabetByString($string);
$count = count($array);
$result = 0;
foreach ( $array as $index => $each){
foreach ($each as $rowIndex => $eachRow){
$result = pow(26,$count - 1);
$array[$index]['result'] = $result * $eachRow;
$count--;
}
}
return array_sum(array_column($array,'result'));
}
/**
* do lookup for every character of a string to find their position on the alphabet table.
*
* @param $string string
* @return array
*/
public function characterLookupOnAlphabetByString($string)
{
$x = $string;
$array = str_split($x);
$result = [];
foreach ($array as $indexOfArrary => $eachArray){
foreach ($this->alphabet as $indexOfAlphabet => $eachAlphabet){
if($eachArray == $eachAlphabet){
$result[$indexOfArrary] = [$eachArray => $indexOfAlphabet 1];
}
}
}
return $result;
}
嗨,伙計們,值得一提的是,因為許多程式和代碼都需要此操作,例如密碼學、暴力攻擊、網路抓取和許多其他操作,這是代碼背后的想法。
使用方法characterLookupOnAlphabetByString我們在字母表上查找字母字符的位置,例如字母表上A的數字1st 字符是字母表上Z的26第 th 個字符,該方法以這種形式回傳結果。
[
{
"x": 24
},
{
"b": 2
},
{
"c": 3
}
]
我已將字串xbc提供給該characterLookupOnAlphabetByString方法,它回傳了上述陣列。到目前為止,我們只是在字母表中找到字串的每個字符的位置。讓我們進入主要操作
接下來,我們將找到字串字符的總迭代次數。使用findCharacterLevelFromString方法。此方法將接收一個字串,然后將其傳遞給該characterLookupOnAlphabetByString方法并接收上述類似陣列的結果,接下來需要查找給定陣列中的字符數。
接下來,我們foreach對回傳的陣列進行回圈,接下來,因為每個字符的數字索引是那個字符,我們不知道在字串中輸入的字符,我們foreach對每個字符進行另一個內部回圈(請檢查陣列結構) , 在內部回圈中,我們找到26(total character - 1) 上的 (這是字母字符的總數) 的冪,然后結果將乘以字母表中字符的位置
uj5u.com熱心網友回復:
只需獲取每個字串的excel列索引并減去它們即可得到距離。
對于字串中的每個新位置,之前的結果將乘以 26,我們將添加當前字符的序號以獲得它的確切狀態值。
片段:
<?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");
在線演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/504160.html
