PHP-değişkenler kaldırmak için güzel bir yol?
Tam bir URL değişkenleri ALMAK da dahil olmak üzere bir dize var. Hangi kaldırmanın en iyi yolu değişkenleri OLSUN. Güzel bir yolu, onları sadece bir çıkarın var mı?
Bu işleri ama çok güzel (bence) bir kod
$current_url = explode('?', $current_url);
echo $current_url[0];
Yukarıdaki kod sadece tüm değişkenleri kaldırır. URL benim durumumda sunucu değişkenleri hakkında herhangi bir bilgiye ihtiyacım yok bu yüzden bir CMS oluşturulur.
CEVAP
Tamam, tüm değişkenleri kaldırmak için, belki de en güzeli
$url = strtok($url, '?');
strtok
here hakkında.
En hızlı (aşağıya bakınız), ve bir URL olmadan kolları '?' düzgün.
Bir url sorgu dizesi almak ve sadece tek bir değişken (bazı durumlarda daha hızlı olabilecek bir düzenli ifade değiştirin kullanmadan) kaldırmak için, gibi bir şey yapabilirsiniz:
function removeqsvar($url, $varname) {
list($urlpart, $qspart) = array_pad(explode('?', $url), 2, '');
parse_str($qspart, $qsvars);
@unset($qsvars[$varname]);
$newqs = http_build_query($qsvars);
return $urlpart . '?' . $newqs;
}
Tek bir var kaldırmak yerine bir düzenli ifade gibi görünebilir:
function removeqsvar($url, $varname) {
return preg_replace('/([?&])'.$varname.'=[^&] (&|$)/','$1',$url);
}
Birkaç farklı yöntemler zamanlamaları işte, zamanlama sağlanması reset arasındakileri çalışır.
<?php
$number_of_tests = 40000;
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] $mtime[0];
$starttime = $mtime;
for($i = 0; $i < $number_of_tests; $i ){
$str = "http://www.example.com?test=test";
preg_replace('/\\?.*/', '', $str);
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "regexp execution time: ".$totaltime." seconds; ";
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] $mtime[0];
$starttime = $mtime;
for($i = 0; $i < $number_of_tests; $i ){
$str = "http://www.example.com?test=test";
$str = explode('?', $str);
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "explode execution time: ".$totaltime." seconds; ";
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] $mtime[0];
$starttime = $mtime;
for($i = 0; $i < $number_of_tests; $i ){
$str = "http://www.example.com?test=test";
$qPos = strpos($str, "?");
$url_without_query_string = substr($str, 0, $qPos);
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "strpos execution time: ".$totaltime." seconds; ";
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] $mtime[0];
$starttime = $mtime;
for($i = 0; $i < $number_of_tests; $i ){
$str = "http://www.example.com?test=test";
$url_without_query_string = strtok($str, '?');
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "tok execution time: ".$totaltime." seconds; ";
gösterir
regexp execution time: 0.14604902267456 seconds; explode execution time: 0.068033933639526 seconds; strpos execution time: 0.064775943756104 seconds; tok execution time: 0.045819044113159 seconds;
regexp execution time: 0.1408839225769 seconds; explode execution time: 0.06751012802124 seconds; strpos execution time: 0.064877986907959 seconds; tok execution time: 0.047760963439941 seconds;
regexp execution time: 0.14162802696228 seconds; explode execution time: 0.065848112106323 seconds; strpos execution time: 0.064821004867554 seconds; tok execution time: 0.041788101196289 seconds;
regexp execution time: 0.14043688774109 seconds; explode execution time: 0.066350221633911 seconds; strpos execution time: 0.066242933273315 seconds; tok execution time: 0.041517972946167 seconds;
regexp execution time: 0.14228296279907 seconds; explode execution time: 0.06665301322937 seconds; strpos execution time: 0.063700199127197 seconds; tok execution time: 0.041836977005005 seconds;
strtok kazanır, ve bugüne kadar en ufak bir kod.
Nasıl bir JavaScript nesnesinin bir öz...
Yerel dizinden silmeden Git deposu bir...
Bir WordPress kullanmak en iyi bir şek...
Nasıl jQuery UI iletişim kutusunda Kap...
Nasıl Python dizin tarafından bir list...