2012-03-24 50 views
-1

Possible Duplicate:
how can i solve “ Deprecated: Function eregi() is deprecated” error
Converting ereg expressions to preg置换弃用eregi

我想简单地输出文件夹中的所有图像阵列我的服务器上。不幸的是,我对php知之甚少。我收到了eregi弃用的错误,我不知道如何解决这个问题。我尝试了preg_match和stripos,可能不正确,但无济于事。帮帮我!

<? 
//PHP SCRIPT: getimages.php 
Header("content-type: application/x-javascript"); 

//This function gets the file names of all images in the current directory 
//and ouputs them as a JavaScript array 
function returnimages($dirname=".") { 
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions 
$files = array(); 
$curimage=0; 
if($handle = opendir($dirname)) { 
while(false !== ($file = readdir($handle))){ 
if(eregi($pattern, $file)){ //if this file is a valid image 
//Output it as a JavaScript array element 
echo 'smootharray['.$curimage.']="'.$file .'";'; 
$curimage++; 
} 
} 

closedir($handle); 
} 
return($files); 
} 

echo 'var smootharray=new Array();'; //Define array in JavaScript 
returnimages() //Output the array elements containing the image file names 
?> 

另外,我在WordPress调用这个就好像它是一个脚本,像这样:

<script type='text/javascript' src='http://www.foxterrier.com/wp-content/themes/shape/smooths/getimages.php?ver=3.3.1'></script> 

然后我使用了一个数组和jQuery的周期插件另一个脚本。它似乎在我的旧服务器上工作,但现在不在新服务器上(可能是由于eregi错误)。这种编码是否有问题,如果是的话,有什么更好的?谢谢!

+0

谢谢凯莉,但我已经看了很多过时eregi线程,我似乎无法弄清楚。 – Zade 2012-03-24 01:27:04

回答

4

您可以改为使用preg_match。 http://www.php.net/manual/en/function.preg-match.php

它有一些不同的语法,但在你的情况下,你的正则表达式模式应该与preg_match一起工作。

例子:

if(preg_match("/(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)/", $file)){ 
    echo "HIT"; 
} 
+0

谢谢。我认为你有太多的结束括号。无论如何,我的新行说: if(preg_match(“/(\。jpg $)|(\。png $)|(\。jpeg $)|(\。gif $)/)”,$ file){ 这给“未捕获的语法错误:意外的令牌<”。如果我切换)“to”)if(preg_match(“/(\。jpg $)|(\。png $)|(\。jpeg $)|(\ .gif $)/”),$文件){ – Zade 2012-03-24 01:25:46

+0

是的,对不起,我有一个错字。我现在要编辑它。 – Gohn67 2012-03-24 01:29:51

+0

好的,我做了编辑,我把最后的报价放在了错误的地方。希望它现在能为你工作。 – Gohn67 2012-03-24 01:31:03