替换已弃用的eregi

时间:2012-03-24 01:10:22

标签: php arrays image eregi

  

可能重复:
  how can i solve “ Deprecated: Function eregi() is deprecated” error
  Converting ereg expressions to preg

我正在尝试在我的服务器上的文件夹中输出所有图像的数组。不幸的是我对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错误)。这种编码是否有问题,如果是,那还有什么好处?谢谢!

1 个答案:

答案 0 :(得分:5)

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

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

示例:

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