0
這是我的代碼:我該如何更快地做到這一點?
$valid = true;
$len = strlen($packet->username);
if($len > 16 or $len < 3){
$valid = false;
}
for($i = 0; $i < $len and $valid; ++$i){
$c = ord($packet->username{$i});
if(($c >= ord("a") and $c <= ord("z")) or
($c >= ord("A") and $c <= ord("Z")) or
($c >= ord("0") and $c <= ord("9")) or $c === ord("_")){
continue;
}
$valid = false;
break;
}
if(!$valid or $this->iusername === "rcon" or $this->iusername === "console"){
$this->close("", "§ePlease make sure your username is longer then §a3§e characters\n§eand only uses numbers and letters§7.");
return;
}
它會檢查用戶名,以確保它是有效的。這段代碼太慢了,它會循環$ len次,這會導致我的主線程中無用的秒數浪費。任何人都知道任何解決方案,使這個更快&可能會刪除循環?
爲什麼不簡單'$ foo = preg_replace('/ [^ A-Za-z0-9 _] /','',$ username); if($ foo!= $ username){die(「Bad char detected」); }' –
哦,那代碼好多了。謝謝。 – Callerap
甚至只是preg_match,如果你沒有字符「badchar-less」版本的樣子。 –