Str_Glob_NoCase() - Creating a case-insensitive glob array for use in glob()

This function is part of a solution when you want to use the PHP glob() function to return a list of files with a certain suffix, but you want the glob to perform matching in a case-insensitive manner.

Originally, I wrote a function that returns this type of pattern:

Input (string array): ["jpg","png"]
Output: "{[jJ][pP][gG],[pP][nN][gG]}"

On Linux, this works fine, but on Windows, the glob() function (using the GLOB_BRACE flag) will not handle this correctly (see PHP bug 69628).

As a result, we have to take this new approach. Be warned, however, that PHP limits the glob expression to 260 characters, so trying to glob more than a few file extensions will quickly exhaust this limitation.

PHP Code

function Str_Glob_NoCase ($Suffix) {
	/*
	Purpose
		Used to create an array of case-insensitive glob subexpressions from a specified file suffix.
		The GLOB_BRACE single-letter glob() option does not work on Windows, so we have to take this approach.
	Parameters
		Suffix (string) = The file suffix we are building an expression with.
	Return (string array)
		Example: Suffix = "jpg"
			Returns: [jpg],[Jpg],[jPg],[JPg],[jpG],[JpG],[jPG],[JPG]
		Returns an empty array if:
			- The specified suffix (Suffix) is zero-length.
	Version History
		1.0 2015.05.13	Tested by Russ Tanner on PHP 5.6.8 Win 7-64.
	*/

	$r = array();

	// Trap: The specified suffix (Suffix) is zero-length. Just return a zero-length string.
	if (strlen($Suffix) === 0) return $r;

	// Create a string with a "0" for each character in the specified Suffix.
	// This acts as a mask where each "1" will force the corresponding character in Suffix to be capitalized.
	$Mask = str_repeat('0', strlen($Suffix));

	// Suffix must begin as all lower case. We do this here instead of inside the loop as an optimization.
	$Suffix = strtolower($Suffix);

	// We need a lowercase copy of the suffix on the array buffer.
	// This won't happen in the loop because the counter begins at 1.
	// This is also an optimization. We don't need to iterate the Mask when no 1's exist.
	$r[] = $Suffix;

	// Main capitalization loop.
	while (true) {
		
		// Increment Mask. We don't need to process with a mask of all zeros.
		$x = base_convert($Mask, 2, 10);
		$x++;
		$Mask = str_pad(base_convert($x, 10, 2), strlen($Mask), "0", STR_PAD_LEFT);

		// Reload xSuffix with a new copy that is all lower case.
		$xSuffix = $Suffix;

		// Loop through all characters in Mask.
		// Capitalize characters in Suffix that have a corresponding "1" in Mask.
		for ($i = 0; $i < strlen($Mask); $i++) {
			// Found a "1". Capitalize corresponding character in Suffix.
			if ($Mask[$i] == '1') $xSuffix[$i] = strtoupper($xSuffix[$i]);
		}

		// Append this version of suffix to our array buffer.
		$r[] = $xSuffix;

		// No more zeros in mask. Nothing more to process. Exit the while-loop now.
		if (strpos($Mask, "0") === false) break;

	}

	return $r;

}

 


The Captian
Today they call you "crazy". Tomorrow they call you "ahead of your time."
Global Skywatch Learn about Chemtrails - You're breathing them now!
OnlyTheBestHerbs.com World-class supplements
Mercury Talk Why you are sick.
OneUp Domains Domains, Hosting, Email
1-800-358-4278 (U.S. & Canada)