Make WordPress Core

Changeset 3198


Ignore:
Timestamp:
11/22/2005 11:39:56 PM (19 years ago)
Author:
ryan
Message:

Add glob() compat for php < 4.3.0

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions-compat.php

    r2247 r3198  
    9090}
    9191
     92/* Added in PHP 4.3.0 */
     93
     94if( !(function_exists('glob')) ):
     95function glob($pattern) {
     96    // get pathname (everything up until the last / or \)
     97    $path=$output=null;
     98//  if(PHP_OS=='WIN32')
     99//      $slash='\\';
     100//  else
     101//      $slash='/';
     102    $slash = '/';
     103    $lastpos=strrpos($pattern,$slash);
     104    if(!($lastpos===false)) {
     105        $path=substr($pattern,0,$lastpos); #negative length means take from the right
     106        $pattern=substr($pattern,$lastpos+1);
     107    } else {
     108        //no dir info, use current dir
     109        $path=getcwd();
     110    }
     111    $handle=@ opendir($path);
     112    if($handle===false)
     113        return false;
     114    while($dir=readdir($handle)) {
     115        if ( '.' == $dir || '..' == $dir )
     116            continue;
     117        if (pattern_match($pattern,$dir))
     118            $output[]=$path . '/' . $dir;
     119    }
     120    closedir($handle);
     121    print_r($output);
     122    if(is_array($output))
     123        return $output;
     124
     125    return false;
     126}
     127
     128function pattern_match($pattern,$string) {
     129    // basically prepare a regular expression
     130    $out=null;
     131    $chunks=explode(';',$pattern);
     132    foreach($chunks as $pattern) {
     133        $escape=array('$','^','.','{','}','(',')','[',']','|');
     134        while(strpos($pattern,'**')!==false)
     135            $pattern=str_replace('**','*',$pattern);
     136        foreach($escape as $probe)
     137            $pattern=str_replace($probe,"\\$probe",$pattern);
     138
     139        $pattern=str_replace('?*','*',
     140        str_replace('*?','*',
     141        str_replace('*',".*",
     142        str_replace('?','.{1,1}',$pattern))));
     143        $out[]=$pattern;
     144    }
     145
     146    if(count($out)==1)
     147        return(eregi("^$out[0]$",$string));
     148    else
     149        foreach($out as $tester)
     150            if(eregi("^$tester$",$string))
     151                return true;
     152    return false;
     153}
     154endif;
     155
    92156?>
Note: See TracChangeset for help on using the changeset viewer.