Ben kendi bir kaç öneri ekleyerek, burada önerilen çeşitli yöntemler üzerinde bazı kriterler koştu - bu her yöntemin 100000 tekrarlamalar için zamanlamaları vardır
int cast : 79.45ms
intval : 394.39ms
strtok : 428.85ms
preg_replace : 604.68ms
substr : 719.92ms
explode : 821.99ms
Int döküm yöntemi, bir mil tarafından kazanır, ama belirtildiği gibi, sen öndeki sıfırları kapalı şerit olacak. Intval, aynı sonucun elde edilmesi için daha düşük bir yöntemdir.
with önde gelen sıfır dize almak için hızlı bir yöntem Strtok ($ str, '_') kullanmak için;
$str="154_timestamp";
$c=100000;
$s=microtime(true);
for ($x=0; $x<$c; $x++)
$n=(int)$str;
printf("int cast : %0.2fms\n", (microtime(true)-$s)*1000);
$s=microtime(true);
for ($x=0; $x<$c; $x++)
$n = current(explode("_", $str));
printf("explode : %0.2fms\n", (microtime(true)-$s)*1000);
$s=microtime(true);
for ($x=0; $x<$c; $x++)
$n = substr($str, 0, strpos($str, '_'));
printf("substr : %0.2fms\n", (microtime(true)-$s)*1000);
$s=microtime(true);
for ($x=0; $x<$c; $x++)
$n = strtok($str, '_');
printf("strtok : %0.2fms\n", (microtime(true)-$s)*1000);
$s=microtime(true);
for ($x=0; $x<$c; $x++)
$n = intval($str);
printf("intval : %0.2fms\n", (microtime(true)-$s)*1000);
$s=microtime(true);
for ($x=0; $x<$c; $x++)
$n = preg_replace("/_[^_]+$/",'',$str);
printf("preg_replace : %0.2fms\n", (microtime(true)-$s)*1000);