2012-03-28 60 views
0

在我的网站我有我的这种格式的网页:从php中获取最大的数字?

www.mysite.com/45.php 
www.mysite.com/81.php 
www.mysite.com/58.php 
www.mysite.com/415.php 

我有顺序的编号。我怎样才能得到在这种情况下最大的数字415并将其存储在var中。我试过这个:

<?php 
for ($urlCheck = 1000000; ; $urlCheck--){ 
    if (file_exists()){ 
     echo "true"; 
     break; 
     } 

    } 

?> 

但我不知道我是如何得到这个东西的工作。

+0

数字是否连续? – Sirko 2012-03-28 07:33:08

+0

是的,它从1开始,我想找到最后一个或最大的数字是什么? – user1200640 2012-03-28 07:35:02

回答

1

//的URL看起来像:http://www.mysite.com //路径是这样的:/家/vhosts/www.mysite.com/public

$path = getcwd(); // get current path (needs to be where 45.php etc is) chdir($path); // go there $files = scandir('.'); // we are looking in the current directory natsort($files); $largest = intval(end($files)); // sample value: "415" $filename = end($files); // if you just want the number from a filename: $number=preg_replace('/[^\d]/','',$filename);

3

当您添加一个页面时,您将不得不在某处保存“当前最大号码”,否则(使用任何试图在现场发现的解决方案),性能将会非常糟糕。

对于一个缓慢的实现,仍然是少慢比别人你可以想出,您可以使用此:

$files = scandir('.'); // assume we are looking in the current directory 
natsort($files); 
$largest = intval(end($files)); // sample value: "415" 
+0

此代码返回值0 – user1200640 2012-03-28 07:52:49

+0

@ user1200640:可能是因为您没有向scandir提供正确的路径。 – Jon 2012-03-28 07:58:07

+0

最大的数字是我想得到的,但它是我不知道的,所以我怎样才能将它传递给参数? – user1200640 2012-03-28 08:00:51