private static function getListLength($text) { $i = 1; $lastI = 0; $foundEnd = false; while ($i < strlen($text)) { $curChar = substr($text, $i, 1); if ($curChar == "i") { $i = strpos($text, "e", $i); if ($i === false) { throw new IllegalArgumentException("BEncoded sublist/dictionary integer has no end"); } $i++; } elseif (is_numeric($curChar)) { $seperatorPos = strpos($text, ":", $i); // Search for the seperator if ($seperatorPos === false) { throw new IllegalArgumentException("BEncoded sublist/dictionary contains a string with no length specified"); } $totalLength = substr($text, $i, $seperatorPos - $i); $i = $seperatorPos + $totalLength + 1; } elseif ($curChar == "d" || $curChar == "l") { $i += BList::getListLength(substr($text, $i)); } elseif ($curChar == "e") { $foundEnd = true; break; } if ($i == $lastI) { throw new IllegalArgumentException("BEncoded sublist/dictionary contains malfomed or unrecognized content"); } $lastI = $i; } if (!$foundEnd) { throw new IllegalArgumentException("BEncoded sublist/dictionary had no end"); } return $i + 1; }