Source for file glob.php
Documentation is available at glob.php
// $Id: glob.php,v 1.7 2007/04/17 10:09:56 arpad Exp $
* @license LGPL - http://www.gnu.org/licenses/lgpl.html
* @copyright 2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net>
* @link http://php.net/glob
* @author Arpad Ray <arpad@php.net>
* @version $Revision: 1.7 $
* @require PHP 3.0.9 (preg_replace)
$return_failure = ($flags & GLOB_NOCHECK) ? array ($pattern) : false;
// build path to scan files
$prefix_length = strcspn($pattern, $wildcards_open);
$sep = ($flags & GLOB_NOESCAPE) ? '[\\\\/]' : '(?:/|\\\\(?![' . $wildcards_open . $wildcards_close . ']))';
if (preg_match('#^(.*)' . $sep . '#', substr($pattern, 0 , $prefix_length), $matches)) {
$convert_patterns = array ('/\{([^{}]*)\}/e', '/\[([^\]]*)\]/e');
$convert_replacements = array (
'php_compat_glob_brace_helper(\'$1\', $flags)',
'php_compat_glob_charclass_helper(\'$1\', $flags)'
$new_pattern = preg_replace($convert_patterns, $convert_replacements, $pattern);
if ($new_pattern == $pattern) {
$pattern = '#^' . $pattern . '\z#';
foreach ($files as $file => $dir) {
// find last matching subpattern
for ($i = 1 , $matchc = count($matches); $i < $matchc; $i++ ) {
$file = array ($file, $rank);
usort($results, ($flags & GLOB_NOSORT) ? 'php_compat_glob_brace_nosort_helper' : 'php_compat_glob_brace_sort_helper');
usort($results, 'php_compat_glob_nosort_helper');
// array_values() for php 4 +
foreach ($results as $result) {
$reindex[] = ($flags & GLOB_BRACE) ? $result[0 ] : $result;
* the flags passed to glob()
* true to scan recursively
* an array of files in the given path where the key is the path,
* and the value is 1 if the file is a directory, 0 if it isn't.
* Returns false on unrecoverable errors, or all errors when
return ($flags & GLOB_ERR) ? false : array ($path);
while (($file = readdir($fp)) !== false ) {
if ($file[0 ] == '.' || $file == '..') {
// array_merge for php 4 +
foreach ($files as $rfile => $rdir) {
$results[$rfile] = $rdir;
* Converts a section of a glob pattern to a PCRE pattern
* the flags passed to glob()
for ($i = 0 , $len = strlen($input); $i < $len; $i++ ) {
$escaped = ($i && $input[$i - 1 ] == '\\' && ($flags & GLOB_NOCHECK == false ));
// skips characters classes and subpatterns, they are escaped in their respective helpers
foreach ($opens as $k => $v) {
if ($c == $v[0 ] && !$escaped) {
$ret .= $escaped ? '*' : '.*';
* the contents of the braces to convert
* the flags passed to glob()
* a PCRE subpattern of alternatives
$alternatives = explode(',', $brace);
for ($i = count($alternatives); $i--; ) {
return '(?:(' . implode(')|(', $alternatives) . '))';
* Converts glob character classes
* the contents of the class to convert
* the flags passed to glob()
if (strpos($class, '-') !== false ) {
$class = strtr($class, array ('-' => '')) . '-';
if (strpos($class, ']') !== false ) {
$class = ']' . strtr($class, array (']' => ''));
if (strpos($class, '^') !== false ) {
$class = '\^' . strtr($class, array ('^' => ''));
return '[' . strtr($class, array ('#' => '\#')) . ']';
* Callback sort function for GLOB_NOSORT
* Sorts first by the base name, then in reverse by the extension
$operands = array (array ('full' => $a), array ('full' => $b));
foreach ($operands as $k => $v) {
$v['pos'] = strrpos($v['full'], '.');
if ($v['pos'] === false ) {
$v['pos'] = strlen($v['full']) - 1;
if ($v['slash'] === false ) {
$v['slash'] = strlen($v['full']) - 1;
$operands[$k]['dir'] = substr($v['full'], 0 , $v['slash']);
$operands[$k]['base'] = substr($v['full'], $v['slash'], $v['pos'] - $v['slash']);
$operands[$k]['ext'] = substr($v['full'], $v['pos'] + 1 );
$dir_cmp = strcmp($operands[0 ]['dir'], $operands[1 ]['dir']);
$base_cmp = strcmp($operands[0 ]['base'], $operands[1 ]['base']);
$ext_cmp = strcmp($operands[0 ]['ext'], $operands[1 ]['ext']);
* Callback sort function for GLOB_BRACE
* Each argument should be an array where the first element is the
* file path, and the second is its rank. The rank is the number of
* alternatives between this match and the beginning of the brace.
return ($a[1 ] < $b[1 ]) ? -1 : 1;
* Callback sort function for (GLOB_BRACE | GLOB_NOSORT)
* Receives the same arguments php_compat_glob_brace_sort_helper()
return ($len_a < $len_b) ? -1 : 1;
return ($a[1 ] < $b[1 ]) ? -1 : 1;
function glob($pattern, $flags = 0 )
Documentation generated on Mon, 11 Mar 2019 15:26:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|